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