Subversion Repositories Kolibri OS

Rev

Rev 6103 | Rev 6660 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. #include <ddk.h>
  2. #include <linux/mm.h>
  3. #include <drm/drmP.h>
  4. #include <drm/i915_drm.h>
  5. #include "i915_drv.h"
  6. #include "intel_drv.h"
  7. #include <linux/hdmi.h>
  8. #include <linux/seq_file.h>
  9. #include <linux/fence.h>
  10. #include "i915_kos32.h"
  11.  
  12. struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags)
  13. {
  14.     struct file *filep;
  15.     int count;
  16.  
  17.     filep = __builtin_malloc(sizeof(*filep));
  18.  
  19.     if(unlikely(filep == NULL))
  20.         return ERR_PTR(-ENOMEM);
  21.  
  22.     count = size / PAGE_SIZE;
  23.  
  24.     filep->pages = kzalloc(sizeof(struct page *) * count, 0);
  25.     if(unlikely(filep->pages == NULL))
  26.     {
  27.         kfree(filep);
  28.         return ERR_PTR(-ENOMEM);
  29.     };
  30.  
  31.     filep->count     = count;
  32.     filep->allocated = 0;
  33.     filep->vma       = NULL;
  34.  
  35. //    printf("%s file %p pages %p count %d\n",
  36. //              __FUNCTION__,filep, filep->pages, count);
  37.  
  38.     return filep;
  39. }
  40.  
  41. struct page *shmem_read_mapping_page_gfp(struct file *filep,
  42.                                          pgoff_t index, gfp_t gfp)
  43. {
  44.     struct page *page;
  45.  
  46.     if(unlikely(index >= filep->count))
  47.         return ERR_PTR(-EINVAL);
  48.  
  49.     page = filep->pages[index];
  50.  
  51.     if(unlikely(page == NULL))
  52.     {
  53.         page = (struct page *)AllocPage();
  54.  
  55.         if(unlikely(page == NULL))
  56.             return ERR_PTR(-ENOMEM);
  57.  
  58.         filep->pages[index] = page;
  59. //        printf("file %p index %d page %x\n", filep, index, page);
  60. //        delay(1);
  61.  
  62.     };
  63.  
  64.     return page;
  65. };
  66.  
  67. unsigned long vm_mmap(struct file *file, unsigned long addr,
  68.          unsigned long len, unsigned long prot,
  69.          unsigned long flag, unsigned long offset)
  70. {
  71.     char *mem, *ptr;
  72.     int i;
  73.  
  74.     if (unlikely(offset + PAGE_ALIGN(len) < offset))
  75.         return -EINVAL;
  76.     if (unlikely(offset & ~PAGE_MASK))
  77.         return -EINVAL;
  78.  
  79.     mem = UserAlloc(len);
  80.     if(unlikely(mem == NULL))
  81.         return -ENOMEM;
  82.  
  83.     for(i = offset, ptr = mem; i < offset+len; i+= 4096, ptr+= 4096)
  84.     {
  85.         struct page *page;
  86.  
  87.         page = shmem_read_mapping_page_gfp(file, i/PAGE_SIZE,0);
  88.  
  89.         if (unlikely(IS_ERR(page)))
  90.             goto err;
  91.  
  92.         MapPage(ptr, (addr_t)page, PG_SHARED|PG_UW);
  93.     }
  94.  
  95.     return (unsigned long)mem;
  96. err:
  97.     UserFree(mem);
  98.     return -ENOMEM;
  99. };
  100.  
  101. void shmem_file_delete(struct file *filep)
  102. {
  103. //    printf("%s file %p pages %p count %d\n",
  104. //            __FUNCTION__, filep, filep->pages, filep->count);
  105.  
  106.     if(filep->pages)
  107.         kfree(filep->pages);
  108. }
  109.  
  110.  
  111.  
  112. static void *check_bytes8(const u8 *start, u8 value, unsigned int bytes)
  113. {
  114.         while (bytes) {
  115.                 if (*start != value)
  116.                         return (void *)start;
  117.                 start++;
  118.                 bytes--;
  119.         }
  120.         return NULL;
  121. }
  122.  
  123. /**
  124.  * memchr_inv - Find an unmatching character in an area of memory.
  125.  * @start: The memory area
  126.  * @c: Find a character other than c
  127.  * @bytes: The size of the area.
  128.  *
  129.  * returns the address of the first character other than @c, or %NULL
  130.  * if the whole buffer contains just @c.
  131.  */
  132. void *memchr_inv(const void *start, int c, size_t bytes)
  133. {
  134.         u8 value = c;
  135.         u64 value64;
  136.         unsigned int words, prefix;
  137.  
  138.         if (bytes <= 16)
  139.                 return check_bytes8(start, value, bytes);
  140.  
  141.         value64 = value;
  142. #if defined(ARCH_HAS_FAST_MULTIPLIER) && BITS_PER_LONG == 64
  143.         value64 *= 0x0101010101010101;
  144. #elif defined(ARCH_HAS_FAST_MULTIPLIER)
  145.         value64 *= 0x01010101;
  146.         value64 |= value64 << 32;
  147. #else
  148.         value64 |= value64 << 8;
  149.         value64 |= value64 << 16;
  150.         value64 |= value64 << 32;
  151. #endif
  152.  
  153.         prefix = (unsigned long)start % 8;
  154.         if (prefix) {
  155.                 u8 *r;
  156.  
  157.                 prefix = 8 - prefix;
  158.                 r = check_bytes8(start, value, prefix);
  159.                 if (r)
  160.                         return r;
  161.                 start += prefix;
  162.                 bytes -= prefix;
  163.         }
  164.  
  165.         words = bytes / 8;
  166.  
  167.         while (words) {
  168.                 if (*(u64 *)start != value64)
  169.                         return check_bytes8(start, value, 8);
  170.                 start += 8;
  171.                 words--;
  172.         }
  173.  
  174.         return check_bytes8(start, value, bytes % 8);
  175. }
  176.  
  177.  
  178.  
  179. int dma_map_sg(struct device *dev, struct scatterlist *sglist,
  180.                            int nelems, int dir)
  181. {
  182.     struct scatterlist *s;
  183.     int i;
  184.  
  185.     for_each_sg(sglist, s, nelems, i) {
  186.         s->dma_address = (dma_addr_t)sg_phys(s);
  187. #ifdef CONFIG_NEED_SG_DMA_LENGTH
  188.         s->dma_length  = s->length;
  189. #endif
  190.     }
  191.  
  192.     return nelems;
  193. }
  194.  
  195.  
  196.  
  197. #define _U  0x01    /* upper */
  198. #define _L  0x02    /* lower */
  199. #define _D  0x04    /* digit */
  200. #define _C  0x08    /* cntrl */
  201. #define _P  0x10    /* punct */
  202. #define _S  0x20    /* white space (space/lf/tab) */
  203. #define _X  0x40    /* hex digit */
  204. #define _SP 0x80    /* hard space (0x20) */
  205.  
  206. extern const unsigned char _ctype[];
  207.  
  208. #define __ismask(x) (_ctype[(int)(unsigned char)(x)])
  209.  
  210. #define isalnum(c)  ((__ismask(c)&(_U|_L|_D)) != 0)
  211. #define isalpha(c)  ((__ismask(c)&(_U|_L)) != 0)
  212. #define iscntrl(c)  ((__ismask(c)&(_C)) != 0)
  213. #define isdigit(c)  ((__ismask(c)&(_D)) != 0)
  214. #define isgraph(c)  ((__ismask(c)&(_P|_U|_L|_D)) != 0)
  215. #define islower(c)  ((__ismask(c)&(_L)) != 0)
  216. #define isprint(c)  ((__ismask(c)&(_P|_U|_L|_D|_SP)) != 0)
  217. #define ispunct(c)  ((__ismask(c)&(_P)) != 0)
  218. /* Note: isspace() must return false for %NUL-terminator */
  219. #define isspace(c)  ((__ismask(c)&(_S)) != 0)
  220. #define isupper(c)  ((__ismask(c)&(_U)) != 0)
  221. #define isxdigit(c) ((__ismask(c)&(_D|_X)) != 0)
  222.  
  223. #define isascii(c) (((unsigned char)(c))<=0x7f)
  224. #define toascii(c) (((unsigned char)(c))&0x7f)
  225.  
  226. static inline unsigned char __tolower(unsigned char c)
  227. {
  228.     if (isupper(c))
  229.         c -= 'A'-'a';
  230.     return c;
  231. }
  232.  
  233. static inline unsigned char __toupper(unsigned char c)
  234. {
  235.     if (islower(c))
  236.         c -= 'a'-'A';
  237.     return c;
  238. }
  239.  
  240. #define tolower(c) __tolower(c)
  241. #define toupper(c) __toupper(c)
  242.  
  243. /*
  244.  * Fast implementation of tolower() for internal usage. Do not use in your
  245.  * code.
  246.  */
  247. static inline char _tolower(const char c)
  248. {
  249.     return c | 0x20;
  250. }
  251.  
  252.  
  253. //const char hex_asc[] = "0123456789abcdef";
  254.  
  255. /**
  256.  * hex_to_bin - convert a hex digit to its real value
  257.  * @ch: ascii character represents hex digit
  258.  *
  259.  * hex_to_bin() converts one hex digit to its actual value or -1 in case of bad
  260.  * input.
  261.  */
  262. int hex_to_bin(char ch)
  263. {
  264.     if ((ch >= '0') && (ch <= '9'))
  265.         return ch - '0';
  266.     ch = tolower(ch);
  267.     if ((ch >= 'a') && (ch <= 'f'))
  268.         return ch - 'a' + 10;
  269.     return -1;
  270. }
  271. EXPORT_SYMBOL(hex_to_bin);
  272.  
  273. /**
  274.  * hex2bin - convert an ascii hexadecimal string to its binary representation
  275.  * @dst: binary result
  276.  * @src: ascii hexadecimal string
  277.  * @count: result length
  278.  *
  279.  * Return 0 on success, -1 in case of bad input.
  280.  */
  281. int hex2bin(u8 *dst, const char *src, size_t count)
  282. {
  283.     while (count--) {
  284.         int hi = hex_to_bin(*src++);
  285.         int lo = hex_to_bin(*src++);
  286.  
  287.         if ((hi < 0) || (lo < 0))
  288.             return -1;
  289.  
  290.         *dst++ = (hi << 4) | lo;
  291.     }
  292.     return 0;
  293. }
  294. EXPORT_SYMBOL(hex2bin);
  295.  
  296. /**
  297.  * hex_dump_to_buffer - convert a blob of data to "hex ASCII" in memory
  298.  * @buf: data blob to dump
  299.  * @len: number of bytes in the @buf
  300.  * @rowsize: number of bytes to print per line; must be 16 or 32
  301.  * @groupsize: number of bytes to print at a time (1, 2, 4, 8; default = 1)
  302.  * @linebuf: where to put the converted data
  303.  * @linebuflen: total size of @linebuf, including space for terminating NUL
  304.  * @ascii: include ASCII after the hex output
  305.  *
  306.  * hex_dump_to_buffer() works on one "line" of output at a time, i.e.,
  307.  * 16 or 32 bytes of input data converted to hex + ASCII output.
  308.  *
  309.  * Given a buffer of u8 data, hex_dump_to_buffer() converts the input data
  310.  * to a hex + ASCII dump at the supplied memory location.
  311.  * The converted output is always NUL-terminated.
  312.  *
  313.  * E.g.:
  314.  *   hex_dump_to_buffer(frame->data, frame->len, 16, 1,
  315.  *          linebuf, sizeof(linebuf), true);
  316.  *
  317.  * example output buffer:
  318.  * 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f  @ABCDEFGHIJKLMNO
  319.  */
  320. int hex_dump_to_buffer(const void *buf, size_t len, int rowsize, int groupsize,
  321.                char *linebuf, size_t linebuflen, bool ascii)
  322. {
  323.     const u8 *ptr = buf;
  324.     int ngroups;
  325.     u8 ch;
  326.     int j, lx = 0;
  327.     int ascii_column;
  328.     int ret;
  329.  
  330.     if (rowsize != 16 && rowsize != 32)
  331.         rowsize = 16;
  332.  
  333.     if (len > rowsize)      /* limit to one line at a time */
  334.         len = rowsize;
  335.     if (!is_power_of_2(groupsize) || groupsize > 8)
  336.         groupsize = 1;
  337.     if ((len % groupsize) != 0) /* no mixed size output */
  338.         groupsize = 1;
  339.  
  340.     ngroups = len / groupsize;
  341.     ascii_column = rowsize * 2 + rowsize / groupsize + 1;
  342.  
  343.     if (!linebuflen)
  344.         goto overflow1;
  345.  
  346.     if (!len)
  347.         goto nil;
  348.  
  349.     if (groupsize == 8) {
  350.         const u64 *ptr8 = buf;
  351.  
  352.         for (j = 0; j < ngroups; j++) {
  353.             ret = snprintf(linebuf + lx, linebuflen - lx,
  354.                        "%s%16.16llx", j ? " " : "",
  355.                        (unsigned long long)*(ptr8 + j));
  356.             if (ret >= linebuflen - lx)
  357.                 goto overflow1;
  358.             lx += ret;
  359.         }
  360.     } else if (groupsize == 4) {
  361.         const u32 *ptr4 = buf;
  362.  
  363.         for (j = 0; j < ngroups; j++) {
  364.             ret = snprintf(linebuf + lx, linebuflen - lx,
  365.                        "%s%8.8x", j ? " " : "",
  366.                        *(ptr4 + j));
  367.             if (ret >= linebuflen - lx)
  368.                 goto overflow1;
  369.             lx += ret;
  370.         }
  371.     } else if (groupsize == 2) {
  372.         const u16 *ptr2 = buf;
  373.  
  374.         for (j = 0; j < ngroups; j++) {
  375.             ret = snprintf(linebuf + lx, linebuflen - lx,
  376.                        "%s%4.4x", j ? " " : "",
  377.                        *(ptr2 + j));
  378.             if (ret >= linebuflen - lx)
  379.                 goto overflow1;
  380.             lx += ret;
  381.         }
  382.     } else {
  383.         for (j = 0; j < len; j++) {
  384.             if (linebuflen < lx + 3)
  385.                 goto overflow2;
  386.             ch = ptr[j];
  387.             linebuf[lx++] = hex_asc_hi(ch);
  388.             linebuf[lx++] = hex_asc_lo(ch);
  389.             linebuf[lx++] = ' ';
  390.         }
  391.         if (j)
  392.             lx--;
  393.     }
  394.     if (!ascii)
  395.         goto nil;
  396.  
  397.     while (lx < ascii_column) {
  398.         if (linebuflen < lx + 2)
  399.             goto overflow2;
  400.         linebuf[lx++] = ' ';
  401.     }
  402.     for (j = 0; j < len; j++) {
  403.         if (linebuflen < lx + 2)
  404.             goto overflow2;
  405.         ch = ptr[j];
  406.         linebuf[lx++] = (isascii(ch) && isprint(ch)) ? ch : '.';
  407.     }
  408. nil:
  409.     linebuf[lx] = '\0';
  410.     return lx;
  411. overflow2:
  412.     linebuf[lx++] = '\0';
  413. overflow1:
  414.     return ascii ? ascii_column + len : (groupsize * 2 + 1) * ngroups - 1;
  415. }
  416. /**
  417.  * print_hex_dump - print a text hex dump to syslog for a binary blob of data
  418.  * @level: kernel log level (e.g. KERN_DEBUG)
  419.  * @prefix_str: string to prefix each line with;
  420.  *  caller supplies trailing spaces for alignment if desired
  421.  * @prefix_type: controls whether prefix of an offset, address, or none
  422.  *  is printed (%DUMP_PREFIX_OFFSET, %DUMP_PREFIX_ADDRESS, %DUMP_PREFIX_NONE)
  423.  * @rowsize: number of bytes to print per line; must be 16 or 32
  424.  * @groupsize: number of bytes to print at a time (1, 2, 4, 8; default = 1)
  425.  * @buf: data blob to dump
  426.  * @len: number of bytes in the @buf
  427.  * @ascii: include ASCII after the hex output
  428.  *
  429.  * Given a buffer of u8 data, print_hex_dump() prints a hex + ASCII dump
  430.  * to the kernel log at the specified kernel log level, with an optional
  431.  * leading prefix.
  432.  *
  433.  * print_hex_dump() works on one "line" of output at a time, i.e.,
  434.  * 16 or 32 bytes of input data converted to hex + ASCII output.
  435.  * print_hex_dump() iterates over the entire input @buf, breaking it into
  436.  * "line size" chunks to format and print.
  437.  *
  438.  * E.g.:
  439.  *   print_hex_dump(KERN_DEBUG, "raw data: ", DUMP_PREFIX_ADDRESS,
  440.  *          16, 1, frame->data, frame->len, true);
  441.  *
  442.  * Example output using %DUMP_PREFIX_OFFSET and 1-byte mode:
  443.  * 0009ab42: 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f  @ABCDEFGHIJKLMNO
  444.  * Example output using %DUMP_PREFIX_ADDRESS and 4-byte mode:
  445.  * ffffffff88089af0: 73727170 77767574 7b7a7978 7f7e7d7c  pqrstuvwxyz{|}~.
  446.  */
  447. void print_hex_dump(const char *level, const char *prefix_str, int prefix_type,
  448.             int rowsize, int groupsize,
  449.             const void *buf, size_t len, bool ascii)
  450. {
  451.     const u8 *ptr = buf;
  452.     int i, linelen, remaining = len;
  453.     unsigned char linebuf[32 * 3 + 2 + 32 + 1];
  454.  
  455.     if (rowsize != 16 && rowsize != 32)
  456.         rowsize = 16;
  457.  
  458.     for (i = 0; i < len; i += rowsize) {
  459.         linelen = min(remaining, rowsize);
  460.         remaining -= rowsize;
  461.  
  462.         hex_dump_to_buffer(ptr + i, linelen, rowsize, groupsize,
  463.                    linebuf, sizeof(linebuf), ascii);
  464.  
  465.         switch (prefix_type) {
  466.         case DUMP_PREFIX_ADDRESS:
  467.             printk("%s%s%p: %s\n",
  468.                    level, prefix_str, ptr + i, linebuf);
  469.             break;
  470.         case DUMP_PREFIX_OFFSET:
  471.             printk("%s%s%.8x: %s\n", level, prefix_str, i, linebuf);
  472.             break;
  473.         default:
  474.             printk("%s%s%s\n", level, prefix_str, linebuf);
  475.             break;
  476.         }
  477.     }
  478. }
  479.  
  480. void print_hex_dump_bytes(const char *prefix_str, int prefix_type,
  481.                           const void *buf, size_t len)
  482. {
  483.     print_hex_dump(KERN_DEBUG, prefix_str, prefix_type, 16, 1,
  484.                        buf, len, true);
  485. }
  486.  
  487. void *kmemdup(const void *src, size_t len, gfp_t gfp)
  488. {
  489.     void *p;
  490.  
  491.     p = kmalloc(len, gfp);
  492.     if (p)
  493.         memcpy(p, src, len);
  494.     return p;
  495. }
  496.  
  497.  
  498. #define KMAP_MAX    256
  499.  
  500. static struct mutex kmap_mutex;
  501. static struct page* kmap_table[KMAP_MAX];
  502. static int kmap_av;
  503. static int kmap_first;
  504. static void* kmap_base;
  505.  
  506.  
  507. int kmap_init()
  508. {
  509.     kmap_base = AllocKernelSpace(KMAP_MAX*4096);
  510.     if(kmap_base == NULL)
  511.         return -1;
  512.  
  513.     kmap_av = KMAP_MAX;
  514.     MutexInit(&kmap_mutex);
  515.     return 0;
  516. };
  517.  
  518. void *kmap(struct page *page)
  519. {
  520.     void *vaddr = NULL;
  521.     int i;
  522.  
  523.     do
  524.     {
  525.         MutexLock(&kmap_mutex);
  526.         if(kmap_av != 0)
  527.         {
  528.             for(i = kmap_first; i < KMAP_MAX; i++)
  529.             {
  530.                 if(kmap_table[i] == NULL)
  531.                 {
  532.                     kmap_av--;
  533.                     kmap_first = i;
  534.                     kmap_table[i] = page;
  535.                     vaddr = kmap_base + (i<<12);
  536.                     MapPage(vaddr,(addr_t)page,3);
  537.                     break;
  538.                 };
  539.             };
  540.         };
  541.         MutexUnlock(&kmap_mutex);
  542.     }while(vaddr == NULL);
  543.  
  544.     return vaddr;
  545. };
  546.  
  547. void *kmap_atomic(struct page *page) __attribute__ ((alias ("kmap")));
  548.  
  549. void kunmap(struct page *page)
  550. {
  551.     void *vaddr;
  552.     int   i;
  553.  
  554.     MutexLock(&kmap_mutex);
  555.  
  556.     for(i = 0; i < KMAP_MAX; i++)
  557.     {
  558.         if(kmap_table[i] == page)
  559.         {
  560.             kmap_av++;
  561.             if(i < kmap_first)
  562.                 kmap_first = i;
  563.             kmap_table[i] = NULL;
  564.             vaddr = kmap_base + (i<<12);
  565.             MapPage(vaddr,0,0);
  566.             break;
  567.         };
  568.     };
  569.  
  570.     MutexUnlock(&kmap_mutex);
  571. };
  572.  
  573. void kunmap_atomic(void *vaddr)
  574. {
  575.     int i;
  576.  
  577.     MapPage(vaddr,0,0);
  578.  
  579.     i = (vaddr - kmap_base) >> 12;
  580.  
  581.     MutexLock(&kmap_mutex);
  582.  
  583.     kmap_av++;
  584.     if(i < kmap_first)
  585.         kmap_first = i;
  586.     kmap_table[i] = NULL;
  587.  
  588.     MutexUnlock(&kmap_mutex);
  589. }
  590.  
  591. void msleep(unsigned int msecs)
  592. {
  593.     msecs /= 10;
  594.     if(!msecs) msecs = 1;
  595.  
  596.      __asm__ __volatile__ (
  597.      "call *__imp__Delay"
  598.      ::"b" (msecs));
  599.      __asm__ __volatile__ (
  600.      "":::"ebx");
  601.  
  602. };
  603.  
  604.  
  605. /* simple loop based delay: */
  606. static void delay_loop(unsigned long loops)
  607. {
  608.         asm volatile(
  609.                 "       test %0,%0      \n"
  610.                 "       jz 3f           \n"
  611.                 "       jmp 1f          \n"
  612.  
  613.                 ".align 16              \n"
  614.                 "1:     jmp 2f          \n"
  615.  
  616.                 ".align 16              \n"
  617.                 "2:     dec %0          \n"
  618.                 "       jnz 2b          \n"
  619.                 "3:     dec %0          \n"
  620.  
  621.                 : /* we don't need output */
  622.                 :"a" (loops)
  623.         );
  624. }
  625.  
  626.  
  627. static void (*delay_fn)(unsigned long) = delay_loop;
  628.  
  629. void __delay(unsigned long loops)
  630. {
  631.         delay_fn(loops);
  632. }
  633.  
  634.  
  635. inline void __const_udelay(unsigned long xloops)
  636. {
  637.         int d0;
  638.  
  639.         xloops *= 4;
  640.         asm("mull %%edx"
  641.                 : "=d" (xloops), "=&a" (d0)
  642.                 : "1" (xloops), ""
  643.                 (loops_per_jiffy * (HZ/4)));
  644.  
  645.         __delay(++xloops);
  646. }
  647.  
  648. void __udelay(unsigned long usecs)
  649. {
  650.         __const_udelay(usecs * 0x000010c7); /* 2**32 / 1000000 (rounded up) */
  651. }
  652.  
  653. unsigned int _sw_hweight32(unsigned int w)
  654. {
  655. #ifdef CONFIG_ARCH_HAS_FAST_MULTIPLIER
  656.         w -= (w >> 1) & 0x55555555;
  657.         w =  (w & 0x33333333) + ((w >> 2) & 0x33333333);
  658.         w =  (w + (w >> 4)) & 0x0f0f0f0f;
  659.         return (w * 0x01010101) >> 24;
  660. #else
  661.         unsigned int res = w - ((w >> 1) & 0x55555555);
  662.         res = (res & 0x33333333) + ((res >> 2) & 0x33333333);
  663.         res = (res + (res >> 4)) & 0x0F0F0F0F;
  664.         res = res + (res >> 8);
  665.         return (res + (res >> 16)) & 0x000000FF;
  666. #endif
  667. }
  668. EXPORT_SYMBOL(_sw_hweight32);
  669.  
  670.  
  671. void usleep_range(unsigned long min, unsigned long max)
  672. {
  673.     udelay(max);
  674. }
  675. EXPORT_SYMBOL(usleep_range);
  676.  
  677.  
  678. static unsigned long round_jiffies_common(unsigned long j, int cpu,
  679.                 bool force_up)
  680. {
  681.         int rem;
  682.         unsigned long original = j;
  683.  
  684.         /*
  685.          * We don't want all cpus firing their timers at once hitting the
  686.          * same lock or cachelines, so we skew each extra cpu with an extra
  687.          * 3 jiffies. This 3 jiffies came originally from the mm/ code which
  688.          * already did this.
  689.          * The skew is done by adding 3*cpunr, then round, then subtract this
  690.          * extra offset again.
  691.          */
  692.         j += cpu * 3;
  693.  
  694.         rem = j % HZ;
  695.  
  696.         /*
  697.          * If the target jiffie is just after a whole second (which can happen
  698.          * due to delays of the timer irq, long irq off times etc etc) then
  699.          * we should round down to the whole second, not up. Use 1/4th second
  700.          * as cutoff for this rounding as an extreme upper bound for this.
  701.          * But never round down if @force_up is set.
  702.          */
  703.         if (rem < HZ/4 && !force_up) /* round down */
  704.                 j = j - rem;
  705.         else /* round up */
  706.                 j = j - rem + HZ;
  707.  
  708.         /* now that we have rounded, subtract the extra skew again */
  709.         j -= cpu * 3;
  710.  
  711.         /*
  712.          * Make sure j is still in the future. Otherwise return the
  713.          * unmodified value.
  714.          */
  715.         return time_is_after_jiffies(j) ? j : original;
  716. }
  717.  
  718.  
  719. unsigned long round_jiffies_up_relative(unsigned long j, int cpu)
  720. {
  721.         unsigned long j0 = jiffies;
  722.  
  723.         /* Use j0 because jiffies might change while we run */
  724.         return round_jiffies_common(j + j0, 0, true) - j0;
  725. }
  726. EXPORT_SYMBOL_GPL(__round_jiffies_up_relative);
  727.  
  728.  
  729. #include <linux/rcupdate.h>
  730.  
  731. struct rcu_ctrlblk {
  732.         struct rcu_head *rcucblist;     /* List of pending callbacks (CBs). */
  733.         struct rcu_head **donetail;     /* ->next pointer of last "done" CB. */
  734.         struct rcu_head **curtail;      /* ->next pointer of last CB. */
  735. //        RCU_TRACE(long qlen);           /* Number of pending CBs. */
  736. //        RCU_TRACE(unsigned long gp_start); /* Start time for stalls. */
  737. //        RCU_TRACE(unsigned long ticks_this_gp); /* Statistic for stalls. */
  738. //        RCU_TRACE(unsigned long jiffies_stall); /* Jiffies at next stall. */
  739. //        RCU_TRACE(const char *name);    /* Name of RCU type. */
  740. };
  741.  
  742. /* Definition for rcupdate control block. */
  743. static struct rcu_ctrlblk rcu_sched_ctrlblk = {
  744.         .donetail       = &rcu_sched_ctrlblk.rcucblist,
  745.         .curtail        = &rcu_sched_ctrlblk.rcucblist,
  746. //        RCU_TRACE(.name = "rcu_sched")
  747. };
  748.  
  749. static void __call_rcu(struct rcu_head *head,
  750.                        void (*func)(struct rcu_head *rcu),
  751.                        struct rcu_ctrlblk *rcp)
  752. {
  753.         unsigned long flags;
  754.  
  755. //        debug_rcu_head_queue(head);
  756.         head->func = func;
  757.         head->next = NULL;
  758.  
  759.         local_irq_save(flags);
  760.         *rcp->curtail = head;
  761.         rcp->curtail = &head->next;
  762. //        RCU_TRACE(rcp->qlen++);
  763.         local_irq_restore(flags);
  764. }
  765.  
  766. /*
  767.  * Post an RCU callback to be invoked after the end of an RCU-sched grace
  768.  * period.  But since we have but one CPU, that would be after any
  769.  * quiescent state.
  770.  */
  771. void call_rcu_sched(struct rcu_head *head, void (*func)(struct rcu_head *rcu))
  772. {
  773.         __call_rcu(head, func, &rcu_sched_ctrlblk);
  774. }
  775.  
  776. int seq_puts(struct seq_file *m, const char *s)
  777. {
  778.     return 0;
  779. };
  780.  
  781. __printf(2, 3) int seq_printf(struct seq_file *m, const char *f, ...)
  782. {
  783.     return 0;
  784. }
  785.  
  786.  
  787. signed long
  788. fence_wait_timeout(struct fence *fence, bool intr, signed long timeout)
  789. {
  790.         signed long ret;
  791.  
  792.         if (WARN_ON(timeout < 0))
  793.                 return -EINVAL;
  794.  
  795. //        trace_fence_wait_start(fence);
  796.         ret = fence->ops->wait(fence, intr, timeout);
  797. //        trace_fence_wait_end(fence);
  798.         return ret;
  799. }
  800.  
  801. void fence_release(struct kref *kref)
  802. {
  803.         struct fence *fence =
  804.                         container_of(kref, struct fence, refcount);
  805.  
  806. //        trace_fence_destroy(fence);
  807.  
  808.         BUG_ON(!list_empty(&fence->cb_list));
  809.  
  810.         if (fence->ops->release)
  811.                 fence->ops->release(fence);
  812.         else
  813.                 fence_free(fence);
  814. }
  815.  
  816. void fence_free(struct fence *fence)
  817. {
  818.         kfree_rcu(fence, rcu);
  819. }
  820. EXPORT_SYMBOL(fence_free);
  821.  
  822.  
  823. ktime_t ktime_get(void)
  824. {
  825.     ktime_t t;
  826.  
  827.     t.tv64 = GetClockNs();
  828.  
  829.     return t;
  830. }
  831.  
  832. char *strdup(const char *str)
  833. {
  834.     size_t len = strlen(str) + 1;
  835.     char *copy = __builtin_malloc(len);
  836.     if (copy)
  837.     {
  838.         memcpy (copy, str, len);
  839.     }
  840.     return copy;
  841. }
  842.  
  843. int split_cmdline(char *cmdline, char **argv)
  844. {
  845.     enum quote_state
  846.     {
  847.         QUOTE_NONE,         /* no " active in current parm       */
  848.         QUOTE_DELIMITER,    /* " was first char and must be last */
  849.         QUOTE_STARTED       /* " was seen, look for a match      */
  850.     };
  851.  
  852.     enum quote_state state;
  853.     unsigned int argc;
  854.     char *p = cmdline;
  855.     char *new_arg, *start;
  856.  
  857.     argc = 0;
  858.  
  859.     for(;;)
  860.     {
  861.         /* skip over spaces and tabs */
  862.         if ( *p )
  863.         {
  864.             while (*p == ' ' || *p == '\t')
  865.                 ++p;
  866.         }
  867.  
  868.         if (*p == '\0')
  869.             break;
  870.  
  871.         state = QUOTE_NONE;
  872.         if( *p == '\"' )
  873.         {
  874.             p++;
  875.             state = QUOTE_DELIMITER;
  876.         }
  877.         new_arg = start = p;
  878.         for (;;)
  879.         {
  880.             if( *p == '\"' )
  881.             {
  882.                 p++;
  883.                 if( state == QUOTE_NONE )
  884.                 {
  885.                     state = QUOTE_STARTED;
  886.                 }
  887.                 else
  888.                 {
  889.                     state = QUOTE_NONE;
  890.                 }
  891.                 continue;
  892.             }
  893.  
  894.             if( *p == ' ' || *p == '\t' )
  895.             {
  896.                 if( state == QUOTE_NONE )
  897.                 {
  898.                     break;
  899.                 }
  900.             }
  901.  
  902.             if( *p == '\0' )
  903.                 break;
  904.  
  905.             if( *p == '\\' )
  906.             {
  907.                 if( p[1] == '\"' )
  908.                 {
  909.                     ++p;
  910.                     if( p[-2] == '\\' )
  911.                     {
  912.                         continue;
  913.                     }
  914.                 }
  915.             }
  916.             if( argv )
  917.             {
  918.                 *(new_arg++) = *p;
  919.             }
  920.             ++p;
  921.         };
  922.  
  923.         if( argv )
  924.         {
  925.             argv[ argc ] = start;
  926.             ++argc;
  927.  
  928.             /*
  929.               The *new = '\0' is req'd in case there was a \" to "
  930.               translation. It must be after the *p check against
  931.               '\0' because new and p could point to the same char
  932.               in which case the scan would be terminated too soon.
  933.             */
  934.  
  935.             if( *p == '\0' )
  936.             {
  937.                 *new_arg = '\0';
  938.                 break;
  939.             }
  940.             *new_arg = '\0';
  941.             ++p;
  942.         }
  943.         else
  944.         {
  945.             ++argc;
  946.             if( *p == '\0' )
  947.             {
  948.                 break;
  949.             }
  950.             ++p;
  951.         }
  952.     }
  953.  
  954.     return argc;
  955. };
  956.  
  957.  
  958. fb_get_options(const char *name, char **option)
  959. {
  960.     char *opt, *options = NULL;
  961.     int retval = 1;
  962.     int name_len;
  963.  
  964.     if(i915.cmdline_mode == NULL)
  965.         return 1;
  966.  
  967.     name_len = __builtin_strlen(name);
  968.  
  969.     if (name_len )
  970.     {
  971.         opt = i915.cmdline_mode;
  972.         if (!__builtin_strncmp(name, opt, name_len) &&
  973.              opt[name_len] == ':')
  974.         {
  975.              options = opt + name_len + 1;
  976.              retval = 0;
  977.         }
  978.     }
  979.  
  980.     if (option)
  981.         *option = options;
  982.  
  983.     return retval;
  984. }
  985.