omap_gem.c 39 KB

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