omap_gem.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530
  1. /*
  2. * drivers/gpu/drm/omapdrm/omap_gem.c
  3. *
  4. * Copyright (C) 2011 Texas Instruments
  5. * Author: Rob Clark <rob.clark@linaro.org>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License version 2 as published by
  9. * the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  14. * more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along with
  17. * this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <linux/shmem_fs.h>
  20. #include <linux/spinlock.h>
  21. #include <linux/pfn_t.h>
  22. #include <drm/drm_vma_manager.h>
  23. #include "omap_drv.h"
  24. #include "omap_dmm_tiler.h"
  25. /*
  26. * GEM buffer object implementation.
  27. */
  28. /* note: we use upper 8 bits of flags for driver-internal flags: */
  29. #define OMAP_BO_DMA 0x01000000 /* actually is physically contiguous */
  30. #define OMAP_BO_EXT_SYNC 0x02000000 /* externally allocated sync object */
  31. #define OMAP_BO_EXT_MEM 0x04000000 /* externally allocated memory */
  32. struct omap_gem_object {
  33. struct drm_gem_object base;
  34. struct list_head mm_list;
  35. uint32_t flags;
  36. /** width/height for tiled formats (rounded up to slot boundaries) */
  37. uint16_t width, height;
  38. /** roll applied when mapping to DMM */
  39. uint32_t roll;
  40. /**
  41. * If buffer is allocated physically contiguous, the OMAP_BO_DMA flag
  42. * is set and the paddr is valid. Also if the buffer is remapped in
  43. * TILER and paddr_cnt > 0, then paddr is valid. But if you are using
  44. * the physical address and OMAP_BO_DMA is not set, then you should
  45. * be going thru omap_gem_{get,put}_paddr() to ensure the mapping is
  46. * not removed from under your feet.
  47. *
  48. * Note that OMAP_BO_SCANOUT is a hint from userspace that DMA capable
  49. * buffer is requested, but doesn't mean that it is. Use the
  50. * OMAP_BO_DMA flag to determine if the buffer has a DMA capable
  51. * physical address.
  52. */
  53. dma_addr_t paddr;
  54. /**
  55. * # of users of paddr
  56. */
  57. uint32_t paddr_cnt;
  58. /**
  59. * tiler block used when buffer is remapped in DMM/TILER.
  60. */
  61. struct tiler_block *block;
  62. /**
  63. * Array of backing pages, if allocated. Note that pages are never
  64. * allocated for buffers originally allocated from contiguous memory
  65. */
  66. struct page **pages;
  67. /** addresses corresponding to pages in above array */
  68. dma_addr_t *addrs;
  69. /**
  70. * Virtual address, if mapped.
  71. */
  72. void *vaddr;
  73. /**
  74. * sync-object allocated on demand (if needed)
  75. *
  76. * Per-buffer sync-object for tracking pending and completed hw/dma
  77. * read and write operations. The layout in memory is dictated by
  78. * the SGX firmware, which uses this information to stall the command
  79. * stream if a surface is not ready yet.
  80. *
  81. * Note that when buffer is used by SGX, the sync-object needs to be
  82. * allocated from a special heap of sync-objects. This way many sync
  83. * objects can be packed in a page, and not waste GPU virtual address
  84. * space. Because of this we have to have a omap_gem_set_sync_object()
  85. * API to allow replacement of the syncobj after it has (potentially)
  86. * already been allocated. A bit ugly but I haven't thought of a
  87. * better alternative.
  88. */
  89. struct {
  90. uint32_t write_pending;
  91. uint32_t write_complete;
  92. uint32_t read_pending;
  93. uint32_t read_complete;
  94. } *sync;
  95. };
  96. #define to_omap_bo(x) container_of(x, struct omap_gem_object, base)
  97. /* To deal with userspace mmap'ings of 2d tiled buffers, which (a) are
  98. * not necessarily pinned in TILER all the time, and (b) when they are
  99. * they are not necessarily page aligned, we reserve one or more small
  100. * regions in each of the 2d containers to use as a user-GART where we
  101. * can create a second page-aligned mapping of parts of the buffer
  102. * being accessed from userspace.
  103. *
  104. * Note that we could optimize slightly when we know that multiple
  105. * tiler containers are backed by the same PAT.. but I'll leave that
  106. * for later..
  107. */
  108. #define NUM_USERGART_ENTRIES 2
  109. struct omap_drm_usergart_entry {
  110. struct tiler_block *block; /* the reserved tiler block */
  111. dma_addr_t paddr;
  112. struct drm_gem_object *obj; /* the current pinned obj */
  113. pgoff_t obj_pgoff; /* page offset of obj currently
  114. mapped in */
  115. };
  116. struct omap_drm_usergart {
  117. struct omap_drm_usergart_entry entry[NUM_USERGART_ENTRIES];
  118. int height; /* height in rows */
  119. int height_shift; /* ilog2(height in rows) */
  120. int slot_shift; /* ilog2(width per slot) */
  121. int stride_pfn; /* stride in pages */
  122. int last; /* index of last used entry */
  123. };
  124. /* -----------------------------------------------------------------------------
  125. * Helpers
  126. */
  127. /** get mmap offset */
  128. static uint64_t mmap_offset(struct drm_gem_object *obj)
  129. {
  130. struct drm_device *dev = obj->dev;
  131. int ret;
  132. size_t size;
  133. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  134. /* Make it mmapable */
  135. size = omap_gem_mmap_size(obj);
  136. ret = drm_gem_create_mmap_offset_size(obj, size);
  137. if (ret) {
  138. dev_err(dev->dev, "could not allocate mmap offset\n");
  139. return 0;
  140. }
  141. return drm_vma_node_offset_addr(&obj->vma_node);
  142. }
  143. /* GEM objects can either be allocated from contiguous memory (in which
  144. * case obj->filp==NULL), or w/ shmem backing (obj->filp!=NULL). But non
  145. * contiguous buffers can be remapped in TILER/DMM if they need to be
  146. * contiguous... but we don't do this all the time to reduce pressure
  147. * on TILER/DMM space when we know at allocation time that the buffer
  148. * will need to be scanned out.
  149. */
  150. static inline bool is_shmem(struct drm_gem_object *obj)
  151. {
  152. return obj->filp != NULL;
  153. }
  154. /* -----------------------------------------------------------------------------
  155. * Eviction
  156. */
  157. static void evict_entry(struct drm_gem_object *obj,
  158. enum tiler_fmt fmt, struct omap_drm_usergart_entry *entry)
  159. {
  160. struct omap_gem_object *omap_obj = to_omap_bo(obj);
  161. struct omap_drm_private *priv = obj->dev->dev_private;
  162. int n = priv->usergart[fmt].height;
  163. size_t size = PAGE_SIZE * n;
  164. loff_t off = mmap_offset(obj) +
  165. (entry->obj_pgoff << PAGE_SHIFT);
  166. const int m = 1 + ((omap_obj->width << fmt) / PAGE_SIZE);
  167. if (m > 1) {
  168. int i;
  169. /* if stride > than PAGE_SIZE then sparse mapping: */
  170. for (i = n; i > 0; i--) {
  171. unmap_mapping_range(obj->dev->anon_inode->i_mapping,
  172. off, PAGE_SIZE, 1);
  173. off += PAGE_SIZE * m;
  174. }
  175. } else {
  176. unmap_mapping_range(obj->dev->anon_inode->i_mapping,
  177. off, size, 1);
  178. }
  179. entry->obj = NULL;
  180. }
  181. /* Evict a buffer from usergart, if it is mapped there */
  182. static void evict(struct drm_gem_object *obj)
  183. {
  184. struct omap_gem_object *omap_obj = to_omap_bo(obj);
  185. struct omap_drm_private *priv = obj->dev->dev_private;
  186. if (omap_obj->flags & OMAP_BO_TILED) {
  187. enum tiler_fmt fmt = gem2fmt(omap_obj->flags);
  188. int i;
  189. for (i = 0; i < NUM_USERGART_ENTRIES; i++) {
  190. struct omap_drm_usergart_entry *entry =
  191. &priv->usergart[fmt].entry[i];
  192. if (entry->obj == obj)
  193. evict_entry(obj, fmt, entry);
  194. }
  195. }
  196. }
  197. /* -----------------------------------------------------------------------------
  198. * Page Management
  199. */
  200. /** ensure backing pages are allocated */
  201. static int omap_gem_attach_pages(struct drm_gem_object *obj)
  202. {
  203. struct drm_device *dev = obj->dev;
  204. struct omap_gem_object *omap_obj = to_omap_bo(obj);
  205. struct page **pages;
  206. int npages = obj->size >> PAGE_SHIFT;
  207. int i, ret;
  208. dma_addr_t *addrs;
  209. WARN_ON(omap_obj->pages);
  210. pages = drm_gem_get_pages(obj);
  211. if (IS_ERR(pages)) {
  212. dev_err(obj->dev->dev, "could not get pages: %ld\n", PTR_ERR(pages));
  213. return PTR_ERR(pages);
  214. }
  215. /* for non-cached buffers, ensure the new pages are clean because
  216. * DSS, GPU, etc. are not cache coherent:
  217. */
  218. if (omap_obj->flags & (OMAP_BO_WC|OMAP_BO_UNCACHED)) {
  219. addrs = kmalloc(npages * sizeof(*addrs), GFP_KERNEL);
  220. if (!addrs) {
  221. ret = -ENOMEM;
  222. goto free_pages;
  223. }
  224. for (i = 0; i < npages; i++) {
  225. addrs[i] = dma_map_page(dev->dev, pages[i],
  226. 0, PAGE_SIZE, DMA_BIDIRECTIONAL);
  227. }
  228. } else {
  229. addrs = kzalloc(npages * sizeof(*addrs), GFP_KERNEL);
  230. if (!addrs) {
  231. ret = -ENOMEM;
  232. goto free_pages;
  233. }
  234. }
  235. omap_obj->addrs = addrs;
  236. omap_obj->pages = pages;
  237. return 0;
  238. free_pages:
  239. drm_gem_put_pages(obj, pages, true, false);
  240. return ret;
  241. }
  242. /* acquire pages when needed (for example, for DMA where physically
  243. * contiguous buffer is not required
  244. */
  245. static int get_pages(struct drm_gem_object *obj, struct page ***pages)
  246. {
  247. struct omap_gem_object *omap_obj = to_omap_bo(obj);
  248. int ret = 0;
  249. if (is_shmem(obj) && !omap_obj->pages) {
  250. ret = omap_gem_attach_pages(obj);
  251. if (ret) {
  252. dev_err(obj->dev->dev, "could not attach pages\n");
  253. return ret;
  254. }
  255. }
  256. /* TODO: even phys-contig.. we should have a list of pages? */
  257. *pages = omap_obj->pages;
  258. return 0;
  259. }
  260. /** release backing pages */
  261. static void omap_gem_detach_pages(struct drm_gem_object *obj)
  262. {
  263. struct omap_gem_object *omap_obj = to_omap_bo(obj);
  264. /* for non-cached buffers, ensure the new pages are clean because
  265. * DSS, GPU, etc. are not cache coherent:
  266. */
  267. if (omap_obj->flags & (OMAP_BO_WC|OMAP_BO_UNCACHED)) {
  268. int i, npages = obj->size >> PAGE_SHIFT;
  269. for (i = 0; i < npages; i++) {
  270. dma_unmap_page(obj->dev->dev, omap_obj->addrs[i],
  271. PAGE_SIZE, DMA_BIDIRECTIONAL);
  272. }
  273. }
  274. kfree(omap_obj->addrs);
  275. omap_obj->addrs = NULL;
  276. drm_gem_put_pages(obj, omap_obj->pages, true, false);
  277. omap_obj->pages = NULL;
  278. }
  279. /* get buffer flags */
  280. uint32_t omap_gem_flags(struct drm_gem_object *obj)
  281. {
  282. return to_omap_bo(obj)->flags;
  283. }
  284. uint64_t omap_gem_mmap_offset(struct drm_gem_object *obj)
  285. {
  286. uint64_t offset;
  287. mutex_lock(&obj->dev->struct_mutex);
  288. offset = mmap_offset(obj);
  289. mutex_unlock(&obj->dev->struct_mutex);
  290. return offset;
  291. }
  292. /** get mmap size */
  293. size_t omap_gem_mmap_size(struct drm_gem_object *obj)
  294. {
  295. struct omap_gem_object *omap_obj = to_omap_bo(obj);
  296. size_t size = obj->size;
  297. if (omap_obj->flags & OMAP_BO_TILED) {
  298. /* for tiled buffers, the virtual size has stride rounded up
  299. * to 4kb.. (to hide the fact that row n+1 might start 16kb or
  300. * 32kb later!). But we don't back the entire buffer with
  301. * pages, only the valid picture part.. so need to adjust for
  302. * this in the size used to mmap and generate mmap offset
  303. */
  304. size = tiler_vsize(gem2fmt(omap_obj->flags),
  305. omap_obj->width, omap_obj->height);
  306. }
  307. return size;
  308. }
  309. /* get tiled size, returns -EINVAL if not tiled buffer */
  310. int omap_gem_tiled_size(struct drm_gem_object *obj, uint16_t *w, uint16_t *h)
  311. {
  312. struct omap_gem_object *omap_obj = to_omap_bo(obj);
  313. if (omap_obj->flags & OMAP_BO_TILED) {
  314. *w = omap_obj->width;
  315. *h = omap_obj->height;
  316. return 0;
  317. }
  318. return -EINVAL;
  319. }
  320. /* -----------------------------------------------------------------------------
  321. * Fault Handling
  322. */
  323. /* Normal handling for the case of faulting in non-tiled buffers */
  324. static int fault_1d(struct drm_gem_object *obj,
  325. struct vm_area_struct *vma, struct vm_fault *vmf)
  326. {
  327. struct omap_gem_object *omap_obj = to_omap_bo(obj);
  328. unsigned long pfn;
  329. pgoff_t pgoff;
  330. /* We don't use vmf->pgoff since that has the fake offset: */
  331. pgoff = ((unsigned long)vmf->virtual_address -
  332. vma->vm_start) >> PAGE_SHIFT;
  333. if (omap_obj->pages) {
  334. omap_gem_cpu_sync(obj, pgoff);
  335. pfn = page_to_pfn(omap_obj->pages[pgoff]);
  336. } else {
  337. BUG_ON(!(omap_obj->flags & OMAP_BO_DMA));
  338. pfn = (omap_obj->paddr >> PAGE_SHIFT) + pgoff;
  339. }
  340. VERB("Inserting %p pfn %lx, pa %lx", vmf->virtual_address,
  341. pfn, pfn << PAGE_SHIFT);
  342. return vm_insert_mixed(vma, (unsigned long)vmf->virtual_address,
  343. __pfn_to_pfn_t(pfn, PFN_DEV));
  344. }
  345. /* Special handling for the case of faulting in 2d tiled buffers */
  346. static int fault_2d(struct drm_gem_object *obj,
  347. struct vm_area_struct *vma, struct vm_fault *vmf)
  348. {
  349. struct omap_gem_object *omap_obj = to_omap_bo(obj);
  350. struct omap_drm_private *priv = obj->dev->dev_private;
  351. struct omap_drm_usergart_entry *entry;
  352. enum tiler_fmt fmt = gem2fmt(omap_obj->flags);
  353. struct page *pages[64]; /* XXX is this too much to have on stack? */
  354. unsigned long pfn;
  355. pgoff_t pgoff, base_pgoff;
  356. void __user *vaddr;
  357. int i, ret, slots;
  358. /*
  359. * Note the height of the slot is also equal to the number of pages
  360. * that need to be mapped in to fill 4kb wide CPU page. If the slot
  361. * height is 64, then 64 pages fill a 4kb wide by 64 row region.
  362. */
  363. const int n = priv->usergart[fmt].height;
  364. const int n_shift = priv->usergart[fmt].height_shift;
  365. /*
  366. * If buffer width in bytes > PAGE_SIZE then the virtual stride is
  367. * rounded up to next multiple of PAGE_SIZE.. this need to be taken
  368. * into account in some of the math, so figure out virtual stride
  369. * in pages
  370. */
  371. const int m = 1 + ((omap_obj->width << fmt) / PAGE_SIZE);
  372. /* We don't use vmf->pgoff since that has the fake offset: */
  373. pgoff = ((unsigned long)vmf->virtual_address -
  374. vma->vm_start) >> PAGE_SHIFT;
  375. /*
  376. * Actual address we start mapping at is rounded down to previous slot
  377. * boundary in the y direction:
  378. */
  379. base_pgoff = round_down(pgoff, m << n_shift);
  380. /* figure out buffer width in slots */
  381. slots = omap_obj->width >> priv->usergart[fmt].slot_shift;
  382. vaddr = vmf->virtual_address - ((pgoff - base_pgoff) << PAGE_SHIFT);
  383. entry = &priv->usergart[fmt].entry[priv->usergart[fmt].last];
  384. /* evict previous buffer using this usergart entry, if any: */
  385. if (entry->obj)
  386. evict_entry(entry->obj, fmt, entry);
  387. entry->obj = obj;
  388. entry->obj_pgoff = base_pgoff;
  389. /* now convert base_pgoff to phys offset from virt offset: */
  390. base_pgoff = (base_pgoff >> n_shift) * slots;
  391. /* for wider-than 4k.. figure out which part of the slot-row we want: */
  392. if (m > 1) {
  393. int off = pgoff % m;
  394. entry->obj_pgoff += off;
  395. base_pgoff /= m;
  396. slots = min(slots - (off << n_shift), n);
  397. base_pgoff += off << n_shift;
  398. vaddr += off << PAGE_SHIFT;
  399. }
  400. /*
  401. * Map in pages. Beyond the valid pixel part of the buffer, we set
  402. * pages[i] to NULL to get a dummy page mapped in.. if someone
  403. * reads/writes it they will get random/undefined content, but at
  404. * least it won't be corrupting whatever other random page used to
  405. * be mapped in, or other undefined behavior.
  406. */
  407. memcpy(pages, &omap_obj->pages[base_pgoff],
  408. sizeof(struct page *) * slots);
  409. memset(pages + slots, 0,
  410. sizeof(struct page *) * (n - slots));
  411. ret = tiler_pin(entry->block, pages, ARRAY_SIZE(pages), 0, true);
  412. if (ret) {
  413. dev_err(obj->dev->dev, "failed to pin: %d\n", ret);
  414. return ret;
  415. }
  416. pfn = entry->paddr >> PAGE_SHIFT;
  417. VERB("Inserting %p pfn %lx, pa %lx", vmf->virtual_address,
  418. pfn, pfn << PAGE_SHIFT);
  419. for (i = n; i > 0; i--) {
  420. vm_insert_mixed(vma, (unsigned long)vaddr,
  421. __pfn_to_pfn_t(pfn, PFN_DEV));
  422. pfn += priv->usergart[fmt].stride_pfn;
  423. vaddr += PAGE_SIZE * m;
  424. }
  425. /* simple round-robin: */
  426. priv->usergart[fmt].last = (priv->usergart[fmt].last + 1)
  427. % NUM_USERGART_ENTRIES;
  428. return 0;
  429. }
  430. /**
  431. * omap_gem_fault - pagefault handler for GEM objects
  432. * @vma: the VMA of the GEM object
  433. * @vmf: fault detail
  434. *
  435. * Invoked when a fault occurs on an mmap of a GEM managed area. GEM
  436. * does most of the work for us including the actual map/unmap calls
  437. * but we need to do the actual page work.
  438. *
  439. * The VMA was set up by GEM. In doing so it also ensured that the
  440. * vma->vm_private_data points to the GEM object that is backing this
  441. * mapping.
  442. */
  443. int omap_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  444. {
  445. struct drm_gem_object *obj = vma->vm_private_data;
  446. struct omap_gem_object *omap_obj = to_omap_bo(obj);
  447. struct drm_device *dev = obj->dev;
  448. struct page **pages;
  449. int ret;
  450. /* Make sure we don't parallel update on a fault, nor move or remove
  451. * something from beneath our feet
  452. */
  453. mutex_lock(&dev->struct_mutex);
  454. /* if a shmem backed object, make sure we have pages attached now */
  455. ret = get_pages(obj, &pages);
  456. if (ret)
  457. goto fail;
  458. /* where should we do corresponding put_pages().. we are mapping
  459. * the original page, rather than thru a GART, so we can't rely
  460. * on eviction to trigger this. But munmap() or all mappings should
  461. * probably trigger put_pages()?
  462. */
  463. if (omap_obj->flags & OMAP_BO_TILED)
  464. ret = fault_2d(obj, vma, vmf);
  465. else
  466. ret = fault_1d(obj, vma, vmf);
  467. fail:
  468. mutex_unlock(&dev->struct_mutex);
  469. switch (ret) {
  470. case 0:
  471. case -ERESTARTSYS:
  472. case -EINTR:
  473. return VM_FAULT_NOPAGE;
  474. case -ENOMEM:
  475. return VM_FAULT_OOM;
  476. default:
  477. return VM_FAULT_SIGBUS;
  478. }
  479. }
  480. /** We override mainly to fix up some of the vm mapping flags.. */
  481. int omap_gem_mmap(struct file *filp, struct vm_area_struct *vma)
  482. {
  483. int ret;
  484. ret = drm_gem_mmap(filp, vma);
  485. if (ret) {
  486. DBG("mmap failed: %d", ret);
  487. return ret;
  488. }
  489. return omap_gem_mmap_obj(vma->vm_private_data, vma);
  490. }
  491. int omap_gem_mmap_obj(struct drm_gem_object *obj,
  492. struct vm_area_struct *vma)
  493. {
  494. struct omap_gem_object *omap_obj = to_omap_bo(obj);
  495. vma->vm_flags &= ~VM_PFNMAP;
  496. vma->vm_flags |= VM_MIXEDMAP;
  497. if (omap_obj->flags & OMAP_BO_WC) {
  498. vma->vm_page_prot = pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
  499. } else if (omap_obj->flags & OMAP_BO_UNCACHED) {
  500. vma->vm_page_prot = pgprot_noncached(vm_get_page_prot(vma->vm_flags));
  501. } else {
  502. /*
  503. * We do have some private objects, at least for scanout buffers
  504. * on hardware without DMM/TILER. But these are allocated write-
  505. * combine
  506. */
  507. if (WARN_ON(!obj->filp))
  508. return -EINVAL;
  509. /*
  510. * Shunt off cached objs to shmem file so they have their own
  511. * address_space (so unmap_mapping_range does what we want,
  512. * in particular in the case of mmap'd dmabufs)
  513. */
  514. fput(vma->vm_file);
  515. vma->vm_pgoff = 0;
  516. vma->vm_file = get_file(obj->filp);
  517. vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
  518. }
  519. return 0;
  520. }
  521. /* -----------------------------------------------------------------------------
  522. * Dumb Buffers
  523. */
  524. /**
  525. * omap_gem_dumb_create - create a dumb buffer
  526. * @drm_file: our client file
  527. * @dev: our device
  528. * @args: the requested arguments copied from userspace
  529. *
  530. * Allocate a buffer suitable for use for a frame buffer of the
  531. * form described by user space. Give userspace a handle by which
  532. * to reference it.
  533. */
  534. int omap_gem_dumb_create(struct drm_file *file, struct drm_device *dev,
  535. struct drm_mode_create_dumb *args)
  536. {
  537. union omap_gem_size gsize;
  538. args->pitch = align_pitch(0, args->width, args->bpp);
  539. args->size = PAGE_ALIGN(args->pitch * args->height);
  540. gsize = (union omap_gem_size){
  541. .bytes = args->size,
  542. };
  543. return omap_gem_new_handle(dev, file, gsize,
  544. OMAP_BO_SCANOUT | OMAP_BO_WC, &args->handle);
  545. }
  546. /**
  547. * omap_gem_dumb_map - buffer mapping for dumb interface
  548. * @file: our drm client file
  549. * @dev: drm device
  550. * @handle: GEM handle to the object (from dumb_create)
  551. *
  552. * Do the necessary setup to allow the mapping of the frame buffer
  553. * into user memory. We don't have to do much here at the moment.
  554. */
  555. int omap_gem_dumb_map_offset(struct drm_file *file, struct drm_device *dev,
  556. uint32_t handle, uint64_t *offset)
  557. {
  558. struct drm_gem_object *obj;
  559. int ret = 0;
  560. /* GEM does all our handle to object mapping */
  561. obj = drm_gem_object_lookup(dev, file, handle);
  562. if (obj == NULL) {
  563. ret = -ENOENT;
  564. goto fail;
  565. }
  566. *offset = omap_gem_mmap_offset(obj);
  567. drm_gem_object_unreference_unlocked(obj);
  568. fail:
  569. return ret;
  570. }
  571. #ifdef CONFIG_DRM_FBDEV_EMULATION
  572. /* Set scrolling position. This allows us to implement fast scrolling
  573. * for console.
  574. *
  575. * Call only from non-atomic contexts.
  576. */
  577. int omap_gem_roll(struct drm_gem_object *obj, uint32_t roll)
  578. {
  579. struct omap_gem_object *omap_obj = to_omap_bo(obj);
  580. uint32_t npages = obj->size >> PAGE_SHIFT;
  581. int ret = 0;
  582. if (roll > npages) {
  583. dev_err(obj->dev->dev, "invalid roll: %d\n", roll);
  584. return -EINVAL;
  585. }
  586. omap_obj->roll = roll;
  587. mutex_lock(&obj->dev->struct_mutex);
  588. /* if we aren't mapped yet, we don't need to do anything */
  589. if (omap_obj->block) {
  590. struct page **pages;
  591. ret = get_pages(obj, &pages);
  592. if (ret)
  593. goto fail;
  594. ret = tiler_pin(omap_obj->block, pages, npages, roll, true);
  595. if (ret)
  596. dev_err(obj->dev->dev, "could not repin: %d\n", ret);
  597. }
  598. fail:
  599. mutex_unlock(&obj->dev->struct_mutex);
  600. return ret;
  601. }
  602. #endif
  603. /* -----------------------------------------------------------------------------
  604. * Memory Management & DMA Sync
  605. */
  606. /**
  607. * shmem buffers that are mapped cached can simulate coherency via using
  608. * page faulting to keep track of dirty pages
  609. */
  610. static inline bool is_cached_coherent(struct drm_gem_object *obj)
  611. {
  612. struct omap_gem_object *omap_obj = to_omap_bo(obj);
  613. return is_shmem(obj) &&
  614. ((omap_obj->flags & OMAP_BO_CACHE_MASK) == OMAP_BO_CACHED);
  615. }
  616. /* Sync the buffer for CPU access.. note pages should already be
  617. * attached, ie. omap_gem_get_pages()
  618. */
  619. void omap_gem_cpu_sync(struct drm_gem_object *obj, int pgoff)
  620. {
  621. struct drm_device *dev = obj->dev;
  622. struct omap_gem_object *omap_obj = to_omap_bo(obj);
  623. if (is_cached_coherent(obj) && omap_obj->addrs[pgoff]) {
  624. dma_unmap_page(dev->dev, omap_obj->addrs[pgoff],
  625. PAGE_SIZE, DMA_BIDIRECTIONAL);
  626. omap_obj->addrs[pgoff] = 0;
  627. }
  628. }
  629. /* sync the buffer for DMA access */
  630. void omap_gem_dma_sync(struct drm_gem_object *obj,
  631. enum dma_data_direction dir)
  632. {
  633. struct drm_device *dev = obj->dev;
  634. struct omap_gem_object *omap_obj = to_omap_bo(obj);
  635. if (is_cached_coherent(obj)) {
  636. int i, npages = obj->size >> PAGE_SHIFT;
  637. struct page **pages = omap_obj->pages;
  638. bool dirty = false;
  639. for (i = 0; i < npages; i++) {
  640. if (!omap_obj->addrs[i]) {
  641. omap_obj->addrs[i] = dma_map_page(dev->dev, pages[i], 0,
  642. PAGE_SIZE, DMA_BIDIRECTIONAL);
  643. dirty = true;
  644. }
  645. }
  646. if (dirty) {
  647. unmap_mapping_range(obj->filp->f_mapping, 0,
  648. omap_gem_mmap_size(obj), 1);
  649. }
  650. }
  651. }
  652. /* Get physical address for DMA.. if 'remap' is true, and the buffer is not
  653. * already contiguous, remap it to pin in physically contiguous memory.. (ie.
  654. * map in TILER)
  655. */
  656. int omap_gem_get_paddr(struct drm_gem_object *obj,
  657. dma_addr_t *paddr, bool remap)
  658. {
  659. struct omap_drm_private *priv = obj->dev->dev_private;
  660. struct omap_gem_object *omap_obj = to_omap_bo(obj);
  661. int ret = 0;
  662. mutex_lock(&obj->dev->struct_mutex);
  663. if (remap && is_shmem(obj) && priv->has_dmm) {
  664. if (omap_obj->paddr_cnt == 0) {
  665. struct page **pages;
  666. uint32_t npages = obj->size >> PAGE_SHIFT;
  667. enum tiler_fmt fmt = gem2fmt(omap_obj->flags);
  668. struct tiler_block *block;
  669. BUG_ON(omap_obj->block);
  670. ret = get_pages(obj, &pages);
  671. if (ret)
  672. goto fail;
  673. if (omap_obj->flags & OMAP_BO_TILED) {
  674. block = tiler_reserve_2d(fmt,
  675. omap_obj->width,
  676. omap_obj->height, 0);
  677. } else {
  678. block = tiler_reserve_1d(obj->size);
  679. }
  680. if (IS_ERR(block)) {
  681. ret = PTR_ERR(block);
  682. dev_err(obj->dev->dev,
  683. "could not remap: %d (%d)\n", ret, fmt);
  684. goto fail;
  685. }
  686. /* TODO: enable async refill.. */
  687. ret = tiler_pin(block, pages, npages,
  688. omap_obj->roll, true);
  689. if (ret) {
  690. tiler_release(block);
  691. dev_err(obj->dev->dev,
  692. "could not pin: %d\n", ret);
  693. goto fail;
  694. }
  695. omap_obj->paddr = tiler_ssptr(block);
  696. omap_obj->block = block;
  697. DBG("got paddr: %pad", &omap_obj->paddr);
  698. }
  699. omap_obj->paddr_cnt++;
  700. *paddr = omap_obj->paddr;
  701. } else if (omap_obj->flags & OMAP_BO_DMA) {
  702. *paddr = omap_obj->paddr;
  703. } else {
  704. ret = -EINVAL;
  705. goto fail;
  706. }
  707. fail:
  708. mutex_unlock(&obj->dev->struct_mutex);
  709. return ret;
  710. }
  711. /* Release physical address, when DMA is no longer being performed.. this
  712. * could potentially unpin and unmap buffers from TILER
  713. */
  714. void omap_gem_put_paddr(struct drm_gem_object *obj)
  715. {
  716. struct omap_gem_object *omap_obj = to_omap_bo(obj);
  717. int ret;
  718. mutex_lock(&obj->dev->struct_mutex);
  719. if (omap_obj->paddr_cnt > 0) {
  720. omap_obj->paddr_cnt--;
  721. if (omap_obj->paddr_cnt == 0) {
  722. ret = tiler_unpin(omap_obj->block);
  723. if (ret) {
  724. dev_err(obj->dev->dev,
  725. "could not unpin pages: %d\n", ret);
  726. }
  727. ret = tiler_release(omap_obj->block);
  728. if (ret) {
  729. dev_err(obj->dev->dev,
  730. "could not release unmap: %d\n", ret);
  731. }
  732. omap_obj->paddr = 0;
  733. omap_obj->block = NULL;
  734. }
  735. }
  736. mutex_unlock(&obj->dev->struct_mutex);
  737. }
  738. /* Get rotated scanout address (only valid if already pinned), at the
  739. * specified orientation and x,y offset from top-left corner of buffer
  740. * (only valid for tiled 2d buffers)
  741. */
  742. int omap_gem_rotated_paddr(struct drm_gem_object *obj, uint32_t orient,
  743. int x, int y, dma_addr_t *paddr)
  744. {
  745. struct omap_gem_object *omap_obj = to_omap_bo(obj);
  746. int ret = -EINVAL;
  747. mutex_lock(&obj->dev->struct_mutex);
  748. if ((omap_obj->paddr_cnt > 0) && omap_obj->block &&
  749. (omap_obj->flags & OMAP_BO_TILED)) {
  750. *paddr = tiler_tsptr(omap_obj->block, orient, x, y);
  751. ret = 0;
  752. }
  753. mutex_unlock(&obj->dev->struct_mutex);
  754. return ret;
  755. }
  756. /* Get tiler stride for the buffer (only valid for 2d tiled buffers) */
  757. int omap_gem_tiled_stride(struct drm_gem_object *obj, uint32_t orient)
  758. {
  759. struct omap_gem_object *omap_obj = to_omap_bo(obj);
  760. int ret = -EINVAL;
  761. if (omap_obj->flags & OMAP_BO_TILED)
  762. ret = tiler_stride(gem2fmt(omap_obj->flags), orient);
  763. return ret;
  764. }
  765. /* if !remap, and we don't have pages backing, then fail, rather than
  766. * increasing the pin count (which we don't really do yet anyways,
  767. * because we don't support swapping pages back out). And 'remap'
  768. * might not be quite the right name, but I wanted to keep it working
  769. * similarly to omap_gem_get_paddr(). Note though that mutex is not
  770. * aquired if !remap (because this can be called in atomic ctxt),
  771. * but probably omap_gem_get_paddr() should be changed to work in the
  772. * same way. If !remap, a matching omap_gem_put_pages() call is not
  773. * required (and should not be made).
  774. */
  775. int omap_gem_get_pages(struct drm_gem_object *obj, struct page ***pages,
  776. bool remap)
  777. {
  778. int ret;
  779. if (!remap) {
  780. struct omap_gem_object *omap_obj = to_omap_bo(obj);
  781. if (!omap_obj->pages)
  782. return -ENOMEM;
  783. *pages = omap_obj->pages;
  784. return 0;
  785. }
  786. mutex_lock(&obj->dev->struct_mutex);
  787. ret = get_pages(obj, pages);
  788. mutex_unlock(&obj->dev->struct_mutex);
  789. return ret;
  790. }
  791. /* release pages when DMA no longer being performed */
  792. int omap_gem_put_pages(struct drm_gem_object *obj)
  793. {
  794. /* do something here if we dynamically attach/detach pages.. at
  795. * least they would no longer need to be pinned if everyone has
  796. * released the pages..
  797. */
  798. return 0;
  799. }
  800. #ifdef CONFIG_DRM_FBDEV_EMULATION
  801. /* Get kernel virtual address for CPU access.. this more or less only
  802. * exists for omap_fbdev. This should be called with struct_mutex
  803. * held.
  804. */
  805. void *omap_gem_vaddr(struct drm_gem_object *obj)
  806. {
  807. struct omap_gem_object *omap_obj = to_omap_bo(obj);
  808. WARN_ON(!mutex_is_locked(&obj->dev->struct_mutex));
  809. if (!omap_obj->vaddr) {
  810. struct page **pages;
  811. int ret = get_pages(obj, &pages);
  812. if (ret)
  813. return ERR_PTR(ret);
  814. omap_obj->vaddr = vmap(pages, obj->size >> PAGE_SHIFT,
  815. VM_MAP, pgprot_writecombine(PAGE_KERNEL));
  816. }
  817. return omap_obj->vaddr;
  818. }
  819. #endif
  820. /* -----------------------------------------------------------------------------
  821. * Power Management
  822. */
  823. #ifdef CONFIG_PM
  824. /* re-pin objects in DMM in resume path: */
  825. int omap_gem_resume(struct device *dev)
  826. {
  827. struct drm_device *drm_dev = dev_get_drvdata(dev);
  828. struct omap_drm_private *priv = drm_dev->dev_private;
  829. struct omap_gem_object *omap_obj;
  830. int ret = 0;
  831. list_for_each_entry(omap_obj, &priv->obj_list, mm_list) {
  832. if (omap_obj->block) {
  833. struct drm_gem_object *obj = &omap_obj->base;
  834. uint32_t npages = obj->size >> PAGE_SHIFT;
  835. WARN_ON(!omap_obj->pages); /* this can't happen */
  836. ret = tiler_pin(omap_obj->block,
  837. omap_obj->pages, npages,
  838. omap_obj->roll, true);
  839. if (ret) {
  840. dev_err(dev, "could not repin: %d\n", ret);
  841. return ret;
  842. }
  843. }
  844. }
  845. return 0;
  846. }
  847. #endif
  848. /* -----------------------------------------------------------------------------
  849. * DebugFS
  850. */
  851. #ifdef CONFIG_DEBUG_FS
  852. void omap_gem_describe(struct drm_gem_object *obj, struct seq_file *m)
  853. {
  854. struct omap_gem_object *omap_obj = to_omap_bo(obj);
  855. uint64_t off;
  856. off = drm_vma_node_start(&obj->vma_node);
  857. seq_printf(m, "%08x: %2d (%2d) %08llx %pad (%2d) %p %4d",
  858. omap_obj->flags, obj->name, obj->refcount.refcount.counter,
  859. off, &omap_obj->paddr, omap_obj->paddr_cnt,
  860. omap_obj->vaddr, omap_obj->roll);
  861. if (omap_obj->flags & OMAP_BO_TILED) {
  862. seq_printf(m, " %dx%d", omap_obj->width, omap_obj->height);
  863. if (omap_obj->block) {
  864. struct tcm_area *area = &omap_obj->block->area;
  865. seq_printf(m, " (%dx%d, %dx%d)",
  866. area->p0.x, area->p0.y,
  867. area->p1.x, area->p1.y);
  868. }
  869. } else {
  870. seq_printf(m, " %d", obj->size);
  871. }
  872. seq_printf(m, "\n");
  873. }
  874. void omap_gem_describe_objects(struct list_head *list, struct seq_file *m)
  875. {
  876. struct omap_gem_object *omap_obj;
  877. int count = 0;
  878. size_t size = 0;
  879. list_for_each_entry(omap_obj, list, mm_list) {
  880. struct drm_gem_object *obj = &omap_obj->base;
  881. seq_printf(m, " ");
  882. omap_gem_describe(obj, m);
  883. count++;
  884. size += obj->size;
  885. }
  886. seq_printf(m, "Total %d objects, %zu bytes\n", count, size);
  887. }
  888. #endif
  889. /* -----------------------------------------------------------------------------
  890. * Buffer Synchronization
  891. */
  892. static DEFINE_SPINLOCK(sync_lock);
  893. struct omap_gem_sync_waiter {
  894. struct list_head list;
  895. struct omap_gem_object *omap_obj;
  896. enum omap_gem_op op;
  897. uint32_t read_target, write_target;
  898. /* notify called w/ sync_lock held */
  899. void (*notify)(void *arg);
  900. void *arg;
  901. };
  902. /* list of omap_gem_sync_waiter.. the notify fxn gets called back when
  903. * the read and/or write target count is achieved which can call a user
  904. * callback (ex. to kick 3d and/or 2d), wakeup blocked task (prep for
  905. * cpu access), etc.
  906. */
  907. static LIST_HEAD(waiters);
  908. static inline bool is_waiting(struct omap_gem_sync_waiter *waiter)
  909. {
  910. struct omap_gem_object *omap_obj = waiter->omap_obj;
  911. if ((waiter->op & OMAP_GEM_READ) &&
  912. (omap_obj->sync->write_complete < waiter->write_target))
  913. return true;
  914. if ((waiter->op & OMAP_GEM_WRITE) &&
  915. (omap_obj->sync->read_complete < waiter->read_target))
  916. return true;
  917. return false;
  918. }
  919. /* macro for sync debug.. */
  920. #define SYNCDBG 0
  921. #define SYNC(fmt, ...) do { if (SYNCDBG) \
  922. printk(KERN_ERR "%s:%d: "fmt"\n", \
  923. __func__, __LINE__, ##__VA_ARGS__); \
  924. } while (0)
  925. static void sync_op_update(void)
  926. {
  927. struct omap_gem_sync_waiter *waiter, *n;
  928. list_for_each_entry_safe(waiter, n, &waiters, list) {
  929. if (!is_waiting(waiter)) {
  930. list_del(&waiter->list);
  931. SYNC("notify: %p", waiter);
  932. waiter->notify(waiter->arg);
  933. kfree(waiter);
  934. }
  935. }
  936. }
  937. static inline int sync_op(struct drm_gem_object *obj,
  938. enum omap_gem_op op, bool start)
  939. {
  940. struct omap_gem_object *omap_obj = to_omap_bo(obj);
  941. int ret = 0;
  942. spin_lock(&sync_lock);
  943. if (!omap_obj->sync) {
  944. omap_obj->sync = kzalloc(sizeof(*omap_obj->sync), GFP_ATOMIC);
  945. if (!omap_obj->sync) {
  946. ret = -ENOMEM;
  947. goto unlock;
  948. }
  949. }
  950. if (start) {
  951. if (op & OMAP_GEM_READ)
  952. omap_obj->sync->read_pending++;
  953. if (op & OMAP_GEM_WRITE)
  954. omap_obj->sync->write_pending++;
  955. } else {
  956. if (op & OMAP_GEM_READ)
  957. omap_obj->sync->read_complete++;
  958. if (op & OMAP_GEM_WRITE)
  959. omap_obj->sync->write_complete++;
  960. sync_op_update();
  961. }
  962. unlock:
  963. spin_unlock(&sync_lock);
  964. return ret;
  965. }
  966. /* it is a bit lame to handle updates in this sort of polling way, but
  967. * in case of PVR, the GPU can directly update read/write complete
  968. * values, and not really tell us which ones it updated.. this also
  969. * means that sync_lock is not quite sufficient. So we'll need to
  970. * do something a bit better when it comes time to add support for
  971. * separate 2d hw..
  972. */
  973. void omap_gem_op_update(void)
  974. {
  975. spin_lock(&sync_lock);
  976. sync_op_update();
  977. spin_unlock(&sync_lock);
  978. }
  979. /* mark the start of read and/or write operation */
  980. int omap_gem_op_start(struct drm_gem_object *obj, enum omap_gem_op op)
  981. {
  982. return sync_op(obj, op, true);
  983. }
  984. int omap_gem_op_finish(struct drm_gem_object *obj, enum omap_gem_op op)
  985. {
  986. return sync_op(obj, op, false);
  987. }
  988. static DECLARE_WAIT_QUEUE_HEAD(sync_event);
  989. static void sync_notify(void *arg)
  990. {
  991. struct task_struct **waiter_task = arg;
  992. *waiter_task = NULL;
  993. wake_up_all(&sync_event);
  994. }
  995. int omap_gem_op_sync(struct drm_gem_object *obj, enum omap_gem_op op)
  996. {
  997. struct omap_gem_object *omap_obj = to_omap_bo(obj);
  998. int ret = 0;
  999. if (omap_obj->sync) {
  1000. struct task_struct *waiter_task = current;
  1001. struct omap_gem_sync_waiter *waiter =
  1002. kzalloc(sizeof(*waiter), GFP_KERNEL);
  1003. if (!waiter)
  1004. return -ENOMEM;
  1005. waiter->omap_obj = omap_obj;
  1006. waiter->op = op;
  1007. waiter->read_target = omap_obj->sync->read_pending;
  1008. waiter->write_target = omap_obj->sync->write_pending;
  1009. waiter->notify = sync_notify;
  1010. waiter->arg = &waiter_task;
  1011. spin_lock(&sync_lock);
  1012. if (is_waiting(waiter)) {
  1013. SYNC("waited: %p", waiter);
  1014. list_add_tail(&waiter->list, &waiters);
  1015. spin_unlock(&sync_lock);
  1016. ret = wait_event_interruptible(sync_event,
  1017. (waiter_task == NULL));
  1018. spin_lock(&sync_lock);
  1019. if (waiter_task) {
  1020. SYNC("interrupted: %p", waiter);
  1021. /* we were interrupted */
  1022. list_del(&waiter->list);
  1023. waiter_task = NULL;
  1024. } else {
  1025. /* freed in sync_op_update() */
  1026. waiter = NULL;
  1027. }
  1028. }
  1029. spin_unlock(&sync_lock);
  1030. kfree(waiter);
  1031. }
  1032. return ret;
  1033. }
  1034. /* call fxn(arg), either synchronously or asynchronously if the op
  1035. * is currently blocked.. fxn() can be called from any context
  1036. *
  1037. * (TODO for now fxn is called back from whichever context calls
  1038. * omap_gem_op_update().. but this could be better defined later
  1039. * if needed)
  1040. *
  1041. * TODO more code in common w/ _sync()..
  1042. */
  1043. int omap_gem_op_async(struct drm_gem_object *obj, enum omap_gem_op op,
  1044. void (*fxn)(void *arg), void *arg)
  1045. {
  1046. struct omap_gem_object *omap_obj = to_omap_bo(obj);
  1047. if (omap_obj->sync) {
  1048. struct omap_gem_sync_waiter *waiter =
  1049. kzalloc(sizeof(*waiter), GFP_ATOMIC);
  1050. if (!waiter)
  1051. return -ENOMEM;
  1052. waiter->omap_obj = omap_obj;
  1053. waiter->op = op;
  1054. waiter->read_target = omap_obj->sync->read_pending;
  1055. waiter->write_target = omap_obj->sync->write_pending;
  1056. waiter->notify = fxn;
  1057. waiter->arg = arg;
  1058. spin_lock(&sync_lock);
  1059. if (is_waiting(waiter)) {
  1060. SYNC("waited: %p", waiter);
  1061. list_add_tail(&waiter->list, &waiters);
  1062. spin_unlock(&sync_lock);
  1063. return 0;
  1064. }
  1065. spin_unlock(&sync_lock);
  1066. kfree(waiter);
  1067. }
  1068. /* no waiting.. */
  1069. fxn(arg);
  1070. return 0;
  1071. }
  1072. /* special API so PVR can update the buffer to use a sync-object allocated
  1073. * from it's sync-obj heap. Only used for a newly allocated (from PVR's
  1074. * perspective) sync-object, so we overwrite the new syncobj w/ values
  1075. * from the already allocated syncobj (if there is one)
  1076. */
  1077. int omap_gem_set_sync_object(struct drm_gem_object *obj, void *syncobj)
  1078. {
  1079. struct omap_gem_object *omap_obj = to_omap_bo(obj);
  1080. int ret = 0;
  1081. spin_lock(&sync_lock);
  1082. if ((omap_obj->flags & OMAP_BO_EXT_SYNC) && !syncobj) {
  1083. /* clearing a previously set syncobj */
  1084. syncobj = kmemdup(omap_obj->sync, sizeof(*omap_obj->sync),
  1085. GFP_ATOMIC);
  1086. if (!syncobj) {
  1087. ret = -ENOMEM;
  1088. goto unlock;
  1089. }
  1090. omap_obj->flags &= ~OMAP_BO_EXT_SYNC;
  1091. omap_obj->sync = syncobj;
  1092. } else if (syncobj && !(omap_obj->flags & OMAP_BO_EXT_SYNC)) {
  1093. /* replacing an existing syncobj */
  1094. if (omap_obj->sync) {
  1095. memcpy(syncobj, omap_obj->sync, sizeof(*omap_obj->sync));
  1096. kfree(omap_obj->sync);
  1097. }
  1098. omap_obj->flags |= OMAP_BO_EXT_SYNC;
  1099. omap_obj->sync = syncobj;
  1100. }
  1101. unlock:
  1102. spin_unlock(&sync_lock);
  1103. return ret;
  1104. }
  1105. /* -----------------------------------------------------------------------------
  1106. * Constructor & Destructor
  1107. */
  1108. /* don't call directly.. called from GEM core when it is time to actually
  1109. * free the object..
  1110. */
  1111. void omap_gem_free_object(struct drm_gem_object *obj)
  1112. {
  1113. struct drm_device *dev = obj->dev;
  1114. struct omap_drm_private *priv = dev->dev_private;
  1115. struct omap_gem_object *omap_obj = to_omap_bo(obj);
  1116. evict(obj);
  1117. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  1118. spin_lock(&priv->list_lock);
  1119. list_del(&omap_obj->mm_list);
  1120. spin_unlock(&priv->list_lock);
  1121. /* this means the object is still pinned.. which really should
  1122. * not happen. I think..
  1123. */
  1124. WARN_ON(omap_obj->paddr_cnt > 0);
  1125. /* don't free externally allocated backing memory */
  1126. if (!(omap_obj->flags & OMAP_BO_EXT_MEM)) {
  1127. if (omap_obj->pages)
  1128. omap_gem_detach_pages(obj);
  1129. if (!is_shmem(obj)) {
  1130. dma_free_wc(dev->dev, obj->size, omap_obj->vaddr,
  1131. omap_obj->paddr);
  1132. } else if (omap_obj->vaddr) {
  1133. vunmap(omap_obj->vaddr);
  1134. }
  1135. }
  1136. /* don't free externally allocated syncobj */
  1137. if (!(omap_obj->flags & OMAP_BO_EXT_SYNC))
  1138. kfree(omap_obj->sync);
  1139. drm_gem_object_release(obj);
  1140. kfree(omap_obj);
  1141. }
  1142. /* GEM buffer object constructor */
  1143. struct drm_gem_object *omap_gem_new(struct drm_device *dev,
  1144. union omap_gem_size gsize, uint32_t flags)
  1145. {
  1146. struct omap_drm_private *priv = dev->dev_private;
  1147. struct omap_gem_object *omap_obj;
  1148. struct drm_gem_object *obj;
  1149. struct address_space *mapping;
  1150. size_t size;
  1151. int ret;
  1152. if (flags & OMAP_BO_TILED) {
  1153. if (!priv->usergart) {
  1154. dev_err(dev->dev, "Tiled buffers require DMM\n");
  1155. return NULL;
  1156. }
  1157. /* tiled buffers are always shmem paged backed.. when they are
  1158. * scanned out, they are remapped into DMM/TILER
  1159. */
  1160. flags &= ~OMAP_BO_SCANOUT;
  1161. /* currently don't allow cached buffers.. there is some caching
  1162. * stuff that needs to be handled better
  1163. */
  1164. flags &= ~(OMAP_BO_CACHED|OMAP_BO_WC|OMAP_BO_UNCACHED);
  1165. flags |= tiler_get_cpu_cache_flags();
  1166. /* align dimensions to slot boundaries... */
  1167. tiler_align(gem2fmt(flags),
  1168. &gsize.tiled.width, &gsize.tiled.height);
  1169. /* ...and calculate size based on aligned dimensions */
  1170. size = tiler_size(gem2fmt(flags),
  1171. gsize.tiled.width, gsize.tiled.height);
  1172. } else {
  1173. size = PAGE_ALIGN(gsize.bytes);
  1174. }
  1175. omap_obj = kzalloc(sizeof(*omap_obj), GFP_KERNEL);
  1176. if (!omap_obj)
  1177. return NULL;
  1178. obj = &omap_obj->base;
  1179. if ((flags & OMAP_BO_SCANOUT) && !priv->has_dmm) {
  1180. /* attempt to allocate contiguous memory if we don't
  1181. * have DMM for remappign discontiguous buffers
  1182. */
  1183. omap_obj->vaddr = dma_alloc_wc(dev->dev, size,
  1184. &omap_obj->paddr, GFP_KERNEL);
  1185. if (!omap_obj->vaddr) {
  1186. kfree(omap_obj);
  1187. return NULL;
  1188. }
  1189. flags |= OMAP_BO_DMA;
  1190. }
  1191. spin_lock(&priv->list_lock);
  1192. list_add(&omap_obj->mm_list, &priv->obj_list);
  1193. spin_unlock(&priv->list_lock);
  1194. omap_obj->flags = flags;
  1195. if (flags & OMAP_BO_TILED) {
  1196. omap_obj->width = gsize.tiled.width;
  1197. omap_obj->height = gsize.tiled.height;
  1198. }
  1199. if (flags & (OMAP_BO_DMA|OMAP_BO_EXT_MEM)) {
  1200. drm_gem_private_object_init(dev, obj, size);
  1201. } else {
  1202. ret = drm_gem_object_init(dev, obj, size);
  1203. if (ret)
  1204. goto fail;
  1205. mapping = file_inode(obj->filp)->i_mapping;
  1206. mapping_set_gfp_mask(mapping, GFP_USER | __GFP_DMA32);
  1207. }
  1208. return obj;
  1209. fail:
  1210. omap_gem_free_object(obj);
  1211. return NULL;
  1212. }
  1213. /* convenience method to construct a GEM buffer object, and userspace handle */
  1214. int omap_gem_new_handle(struct drm_device *dev, struct drm_file *file,
  1215. union omap_gem_size gsize, uint32_t flags, uint32_t *handle)
  1216. {
  1217. struct drm_gem_object *obj;
  1218. int ret;
  1219. obj = omap_gem_new(dev, gsize, flags);
  1220. if (!obj)
  1221. return -ENOMEM;
  1222. ret = drm_gem_handle_create(file, obj, handle);
  1223. if (ret) {
  1224. omap_gem_free_object(obj);
  1225. return ret;
  1226. }
  1227. /* drop reference from allocate - handle holds it now */
  1228. drm_gem_object_unreference_unlocked(obj);
  1229. return 0;
  1230. }
  1231. /* -----------------------------------------------------------------------------
  1232. * Init & Cleanup
  1233. */
  1234. /* If DMM is used, we need to set some stuff up.. */
  1235. void omap_gem_init(struct drm_device *dev)
  1236. {
  1237. struct omap_drm_private *priv = dev->dev_private;
  1238. struct omap_drm_usergart *usergart;
  1239. const enum tiler_fmt fmts[] = {
  1240. TILFMT_8BIT, TILFMT_16BIT, TILFMT_32BIT
  1241. };
  1242. int i, j;
  1243. if (!dmm_is_available()) {
  1244. /* DMM only supported on OMAP4 and later, so this isn't fatal */
  1245. dev_warn(dev->dev, "DMM not available, disable DMM support\n");
  1246. return;
  1247. }
  1248. usergart = kcalloc(3, sizeof(*usergart), GFP_KERNEL);
  1249. if (!usergart)
  1250. return;
  1251. /* reserve 4k aligned/wide regions for userspace mappings: */
  1252. for (i = 0; i < ARRAY_SIZE(fmts); i++) {
  1253. uint16_t h = 1, w = PAGE_SIZE >> i;
  1254. tiler_align(fmts[i], &w, &h);
  1255. /* note: since each region is 1 4kb page wide, and minimum
  1256. * number of rows, the height ends up being the same as the
  1257. * # of pages in the region
  1258. */
  1259. usergart[i].height = h;
  1260. usergart[i].height_shift = ilog2(h);
  1261. usergart[i].stride_pfn = tiler_stride(fmts[i], 0) >> PAGE_SHIFT;
  1262. usergart[i].slot_shift = ilog2((PAGE_SIZE / h) >> i);
  1263. for (j = 0; j < NUM_USERGART_ENTRIES; j++) {
  1264. struct omap_drm_usergart_entry *entry;
  1265. struct tiler_block *block;
  1266. entry = &usergart[i].entry[j];
  1267. block = tiler_reserve_2d(fmts[i], w, h, PAGE_SIZE);
  1268. if (IS_ERR(block)) {
  1269. dev_err(dev->dev,
  1270. "reserve failed: %d, %d, %ld\n",
  1271. i, j, PTR_ERR(block));
  1272. return;
  1273. }
  1274. entry->paddr = tiler_ssptr(block);
  1275. entry->block = block;
  1276. DBG("%d:%d: %dx%d: paddr=%pad stride=%d", i, j, w, h,
  1277. &entry->paddr,
  1278. usergart[i].stride_pfn << PAGE_SHIFT);
  1279. }
  1280. }
  1281. priv->usergart = usergart;
  1282. priv->has_dmm = true;
  1283. }
  1284. void omap_gem_deinit(struct drm_device *dev)
  1285. {
  1286. struct omap_drm_private *priv = dev->dev_private;
  1287. /* I believe we can rely on there being no more outstanding GEM
  1288. * objects which could depend on usergart/dmm at this point.
  1289. */
  1290. kfree(priv->usergart);
  1291. }