omap_gem.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507
  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 "omap_drv.h"
  22. #include "omap_dmm_tiler.h"
  23. /* remove these once drm core helpers are merged */
  24. struct page **_drm_gem_get_pages(struct drm_gem_object *obj, gfp_t gfpmask);
  25. void _drm_gem_put_pages(struct drm_gem_object *obj, struct page **pages,
  26. bool dirty, bool accessed);
  27. int _drm_gem_create_mmap_offset_size(struct drm_gem_object *obj, size_t size);
  28. /*
  29. * GEM buffer object implementation.
  30. */
  31. #define to_omap_bo(x) container_of(x, struct omap_gem_object, base)
  32. /* note: we use upper 8 bits of flags for driver-internal flags: */
  33. #define OMAP_BO_DMA 0x01000000 /* actually is physically contiguous */
  34. #define OMAP_BO_EXT_SYNC 0x02000000 /* externally allocated sync object */
  35. #define OMAP_BO_EXT_MEM 0x04000000 /* externally allocated memory */
  36. struct omap_gem_object {
  37. struct drm_gem_object base;
  38. struct list_head mm_list;
  39. uint32_t flags;
  40. /** width/height for tiled formats (rounded up to slot boundaries) */
  41. uint16_t width, height;
  42. /** roll applied when mapping to DMM */
  43. uint32_t roll;
  44. /**
  45. * If buffer is allocated physically contiguous, the OMAP_BO_DMA flag
  46. * is set and the paddr is valid. Also if the buffer is remapped in
  47. * TILER and paddr_cnt > 0, then paddr is valid. But if you are using
  48. * the physical address and OMAP_BO_DMA is not set, then you should
  49. * be going thru omap_gem_{get,put}_paddr() to ensure the mapping is
  50. * not removed from under your feet.
  51. *
  52. * Note that OMAP_BO_SCANOUT is a hint from userspace that DMA capable
  53. * buffer is requested, but doesn't mean that it is. Use the
  54. * OMAP_BO_DMA flag to determine if the buffer has a DMA capable
  55. * physical address.
  56. */
  57. dma_addr_t paddr;
  58. /**
  59. * # of users of paddr
  60. */
  61. uint32_t paddr_cnt;
  62. /**
  63. * tiler block used when buffer is remapped in DMM/TILER.
  64. */
  65. struct tiler_block *block;
  66. /**
  67. * Array of backing pages, if allocated. Note that pages are never
  68. * allocated for buffers originally allocated from contiguous memory
  69. */
  70. struct page **pages;
  71. /** addresses corresponding to pages in above array */
  72. dma_addr_t *addrs;
  73. /**
  74. * Virtual address, if mapped.
  75. */
  76. void *vaddr;
  77. /**
  78. * sync-object allocated on demand (if needed)
  79. *
  80. * Per-buffer sync-object for tracking pending and completed hw/dma
  81. * read and write operations. The layout in memory is dictated by
  82. * the SGX firmware, which uses this information to stall the command
  83. * stream if a surface is not ready yet.
  84. *
  85. * Note that when buffer is used by SGX, the sync-object needs to be
  86. * allocated from a special heap of sync-objects. This way many sync
  87. * objects can be packed in a page, and not waste GPU virtual address
  88. * space. Because of this we have to have a omap_gem_set_sync_object()
  89. * API to allow replacement of the syncobj after it has (potentially)
  90. * already been allocated. A bit ugly but I haven't thought of a
  91. * better alternative.
  92. */
  93. struct {
  94. uint32_t write_pending;
  95. uint32_t write_complete;
  96. uint32_t read_pending;
  97. uint32_t read_complete;
  98. } *sync;
  99. };
  100. static int get_pages(struct drm_gem_object *obj, struct page ***pages);
  101. static uint64_t mmap_offset(struct drm_gem_object *obj);
  102. /* To deal with userspace mmap'ings of 2d tiled buffers, which (a) are
  103. * not necessarily pinned in TILER all the time, and (b) when they are
  104. * they are not necessarily page aligned, we reserve one or more small
  105. * regions in each of the 2d containers to use as a user-GART where we
  106. * can create a second page-aligned mapping of parts of the buffer
  107. * being accessed from userspace.
  108. *
  109. * Note that we could optimize slightly when we know that multiple
  110. * tiler containers are backed by the same PAT.. but I'll leave that
  111. * for later..
  112. */
  113. #define NUM_USERGART_ENTRIES 2
  114. struct usergart_entry {
  115. struct tiler_block *block; /* the reserved tiler block */
  116. dma_addr_t paddr;
  117. struct drm_gem_object *obj; /* the current pinned obj */
  118. pgoff_t obj_pgoff; /* page offset of obj currently
  119. mapped in */
  120. };
  121. static struct {
  122. struct usergart_entry entry[NUM_USERGART_ENTRIES];
  123. int height; /* height in rows */
  124. int height_shift; /* ilog2(height in rows) */
  125. int slot_shift; /* ilog2(width per slot) */
  126. int stride_pfn; /* stride in pages */
  127. int last; /* index of last used entry */
  128. } *usergart;
  129. static void evict_entry(struct drm_gem_object *obj,
  130. enum tiler_fmt fmt, struct usergart_entry *entry)
  131. {
  132. if (obj->dev->dev_mapping) {
  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->dev_mapping,
  144. off, PAGE_SIZE, 1);
  145. off += PAGE_SIZE * m;
  146. }
  147. } else {
  148. unmap_mapping_range(obj->dev->dev_mapping, off, size, 1);
  149. }
  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. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  266. if (!obj->map_list.map) {
  267. /* Make it mmapable */
  268. size_t size = omap_gem_mmap_size(obj);
  269. int ret = _drm_gem_create_mmap_offset_size(obj, size);
  270. if (ret) {
  271. dev_err(dev->dev, "could not allocate mmap offset\n");
  272. return 0;
  273. }
  274. }
  275. return (uint64_t)obj->map_list.hash.key << PAGE_SHIFT;
  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_destroy - destroy a dumb buffer
  532. * @file: client file
  533. * @dev: our DRM device
  534. * @handle: the object handle
  535. *
  536. * Destroy a handle that was created via omap_gem_dumb_create.
  537. */
  538. int omap_gem_dumb_destroy(struct drm_file *file, struct drm_device *dev,
  539. uint32_t handle)
  540. {
  541. /* No special work needed, drop the reference and see what falls out */
  542. return drm_gem_handle_delete(file, handle);
  543. }
  544. /**
  545. * omap_gem_dumb_map - buffer mapping for dumb interface
  546. * @file: our drm client file
  547. * @dev: drm device
  548. * @handle: GEM handle to the object (from dumb_create)
  549. *
  550. * Do the necessary setup to allow the mapping of the frame buffer
  551. * into user memory. We don't have to do much here at the moment.
  552. */
  553. int omap_gem_dumb_map_offset(struct drm_file *file, struct drm_device *dev,
  554. uint32_t handle, uint64_t *offset)
  555. {
  556. struct drm_gem_object *obj;
  557. int ret = 0;
  558. /* GEM does all our handle to object mapping */
  559. obj = drm_gem_object_lookup(dev, file, handle);
  560. if (obj == NULL) {
  561. ret = -ENOENT;
  562. goto fail;
  563. }
  564. *offset = omap_gem_mmap_offset(obj);
  565. drm_gem_object_unreference_unlocked(obj);
  566. fail:
  567. return ret;
  568. }
  569. /* Set scrolling position. This allows us to implement fast scrolling
  570. * for console.
  571. *
  572. * Call only from non-atomic contexts.
  573. */
  574. int omap_gem_roll(struct drm_gem_object *obj, uint32_t roll)
  575. {
  576. struct omap_gem_object *omap_obj = to_omap_bo(obj);
  577. uint32_t npages = obj->size >> PAGE_SHIFT;
  578. int ret = 0;
  579. if (roll > npages) {
  580. dev_err(obj->dev->dev, "invalid roll: %d\n", roll);
  581. return -EINVAL;
  582. }
  583. omap_obj->roll = roll;
  584. mutex_lock(&obj->dev->struct_mutex);
  585. /* if we aren't mapped yet, we don't need to do anything */
  586. if (omap_obj->block) {
  587. struct page **pages;
  588. ret = get_pages(obj, &pages);
  589. if (ret)
  590. goto fail;
  591. ret = tiler_pin(omap_obj->block, pages, npages, roll, true);
  592. if (ret)
  593. dev_err(obj->dev->dev, "could not repin: %d\n", ret);
  594. }
  595. fail:
  596. mutex_unlock(&obj->dev->struct_mutex);
  597. return ret;
  598. }
  599. /* Sync the buffer for CPU access.. note pages should already be
  600. * attached, ie. omap_gem_get_pages()
  601. */
  602. void omap_gem_cpu_sync(struct drm_gem_object *obj, int pgoff)
  603. {
  604. struct drm_device *dev = obj->dev;
  605. struct omap_gem_object *omap_obj = to_omap_bo(obj);
  606. if (is_cached_coherent(obj) && omap_obj->addrs[pgoff]) {
  607. dma_unmap_page(dev->dev, omap_obj->addrs[pgoff],
  608. PAGE_SIZE, DMA_BIDIRECTIONAL);
  609. omap_obj->addrs[pgoff] = 0;
  610. }
  611. }
  612. /* sync the buffer for DMA access */
  613. void omap_gem_dma_sync(struct drm_gem_object *obj,
  614. enum dma_data_direction dir)
  615. {
  616. struct drm_device *dev = obj->dev;
  617. struct omap_gem_object *omap_obj = to_omap_bo(obj);
  618. if (is_cached_coherent(obj)) {
  619. int i, npages = obj->size >> PAGE_SHIFT;
  620. struct page **pages = omap_obj->pages;
  621. bool dirty = false;
  622. for (i = 0; i < npages; i++) {
  623. if (!omap_obj->addrs[i]) {
  624. omap_obj->addrs[i] = dma_map_page(dev->dev, pages[i], 0,
  625. PAGE_SIZE, DMA_BIDIRECTIONAL);
  626. dirty = true;
  627. }
  628. }
  629. if (dirty) {
  630. unmap_mapping_range(obj->filp->f_mapping, 0,
  631. omap_gem_mmap_size(obj), 1);
  632. }
  633. }
  634. }
  635. /* Get physical address for DMA.. if 'remap' is true, and the buffer is not
  636. * already contiguous, remap it to pin in physically contiguous memory.. (ie.
  637. * map in TILER)
  638. */
  639. int omap_gem_get_paddr(struct drm_gem_object *obj,
  640. dma_addr_t *paddr, bool remap)
  641. {
  642. struct omap_drm_private *priv = obj->dev->dev_private;
  643. struct omap_gem_object *omap_obj = to_omap_bo(obj);
  644. int ret = 0;
  645. mutex_lock(&obj->dev->struct_mutex);
  646. if (remap && is_shmem(obj) && priv->has_dmm) {
  647. if (omap_obj->paddr_cnt == 0) {
  648. struct page **pages;
  649. uint32_t npages = obj->size >> PAGE_SHIFT;
  650. enum tiler_fmt fmt = gem2fmt(omap_obj->flags);
  651. struct tiler_block *block;
  652. BUG_ON(omap_obj->block);
  653. ret = get_pages(obj, &pages);
  654. if (ret)
  655. goto fail;
  656. if (omap_obj->flags & OMAP_BO_TILED) {
  657. block = tiler_reserve_2d(fmt,
  658. omap_obj->width,
  659. omap_obj->height, 0);
  660. } else {
  661. block = tiler_reserve_1d(obj->size);
  662. }
  663. if (IS_ERR(block)) {
  664. ret = PTR_ERR(block);
  665. dev_err(obj->dev->dev,
  666. "could not remap: %d (%d)\n", ret, fmt);
  667. goto fail;
  668. }
  669. /* TODO: enable async refill.. */
  670. ret = tiler_pin(block, pages, npages,
  671. omap_obj->roll, true);
  672. if (ret) {
  673. tiler_release(block);
  674. dev_err(obj->dev->dev,
  675. "could not pin: %d\n", ret);
  676. goto fail;
  677. }
  678. omap_obj->paddr = tiler_ssptr(block);
  679. omap_obj->block = block;
  680. DBG("got paddr: %08x", omap_obj->paddr);
  681. }
  682. omap_obj->paddr_cnt++;
  683. *paddr = omap_obj->paddr;
  684. } else if (omap_obj->flags & OMAP_BO_DMA) {
  685. *paddr = omap_obj->paddr;
  686. } else {
  687. ret = -EINVAL;
  688. goto fail;
  689. }
  690. fail:
  691. mutex_unlock(&obj->dev->struct_mutex);
  692. return ret;
  693. }
  694. /* Release physical address, when DMA is no longer being performed.. this
  695. * could potentially unpin and unmap buffers from TILER
  696. */
  697. int omap_gem_put_paddr(struct drm_gem_object *obj)
  698. {
  699. struct omap_gem_object *omap_obj = to_omap_bo(obj);
  700. int ret = 0;
  701. mutex_lock(&obj->dev->struct_mutex);
  702. if (omap_obj->paddr_cnt > 0) {
  703. omap_obj->paddr_cnt--;
  704. if (omap_obj->paddr_cnt == 0) {
  705. ret = tiler_unpin(omap_obj->block);
  706. if (ret) {
  707. dev_err(obj->dev->dev,
  708. "could not unpin pages: %d\n", ret);
  709. goto fail;
  710. }
  711. ret = tiler_release(omap_obj->block);
  712. if (ret) {
  713. dev_err(obj->dev->dev,
  714. "could not release unmap: %d\n", ret);
  715. }
  716. omap_obj->block = NULL;
  717. }
  718. }
  719. fail:
  720. mutex_unlock(&obj->dev->struct_mutex);
  721. return ret;
  722. }
  723. /* Get rotated scanout address (only valid if already pinned), at the
  724. * specified orientation and x,y offset from top-left corner of buffer
  725. * (only valid for tiled 2d buffers)
  726. */
  727. int omap_gem_rotated_paddr(struct drm_gem_object *obj, uint32_t orient,
  728. int x, int y, dma_addr_t *paddr)
  729. {
  730. struct omap_gem_object *omap_obj = to_omap_bo(obj);
  731. int ret = -EINVAL;
  732. mutex_lock(&obj->dev->struct_mutex);
  733. if ((omap_obj->paddr_cnt > 0) && omap_obj->block &&
  734. (omap_obj->flags & OMAP_BO_TILED)) {
  735. *paddr = tiler_tsptr(omap_obj->block, orient, x, y);
  736. ret = 0;
  737. }
  738. mutex_unlock(&obj->dev->struct_mutex);
  739. return ret;
  740. }
  741. /* Get tiler stride for the buffer (only valid for 2d tiled buffers) */
  742. int omap_gem_tiled_stride(struct drm_gem_object *obj, uint32_t orient)
  743. {
  744. struct omap_gem_object *omap_obj = to_omap_bo(obj);
  745. int ret = -EINVAL;
  746. if (omap_obj->flags & OMAP_BO_TILED)
  747. ret = tiler_stride(gem2fmt(omap_obj->flags), orient);
  748. return ret;
  749. }
  750. /* acquire pages when needed (for example, for DMA where physically
  751. * contiguous buffer is not required
  752. */
  753. static int get_pages(struct drm_gem_object *obj, struct page ***pages)
  754. {
  755. struct omap_gem_object *omap_obj = to_omap_bo(obj);
  756. int ret = 0;
  757. if (is_shmem(obj) && !omap_obj->pages) {
  758. ret = omap_gem_attach_pages(obj);
  759. if (ret) {
  760. dev_err(obj->dev->dev, "could not attach pages\n");
  761. return ret;
  762. }
  763. }
  764. /* TODO: even phys-contig.. we should have a list of pages? */
  765. *pages = omap_obj->pages;
  766. return 0;
  767. }
  768. /* if !remap, and we don't have pages backing, then fail, rather than
  769. * increasing the pin count (which we don't really do yet anyways,
  770. * because we don't support swapping pages back out). And 'remap'
  771. * might not be quite the right name, but I wanted to keep it working
  772. * similarly to omap_gem_get_paddr(). Note though that mutex is not
  773. * aquired if !remap (because this can be called in atomic ctxt),
  774. * but probably omap_gem_get_paddr() should be changed to work in the
  775. * same way. If !remap, a matching omap_gem_put_pages() call is not
  776. * required (and should not be made).
  777. */
  778. int omap_gem_get_pages(struct drm_gem_object *obj, struct page ***pages,
  779. bool remap)
  780. {
  781. int ret;
  782. if (!remap) {
  783. struct omap_gem_object *omap_obj = to_omap_bo(obj);
  784. if (!omap_obj->pages)
  785. return -ENOMEM;
  786. *pages = omap_obj->pages;
  787. return 0;
  788. }
  789. mutex_lock(&obj->dev->struct_mutex);
  790. ret = get_pages(obj, pages);
  791. mutex_unlock(&obj->dev->struct_mutex);
  792. return ret;
  793. }
  794. /* release pages when DMA no longer being performed */
  795. int omap_gem_put_pages(struct drm_gem_object *obj)
  796. {
  797. /* do something here if we dynamically attach/detach pages.. at
  798. * least they would no longer need to be pinned if everyone has
  799. * released the pages..
  800. */
  801. return 0;
  802. }
  803. /* Get kernel virtual address for CPU access.. this more or less only
  804. * exists for omap_fbdev. This should be called with struct_mutex
  805. * held.
  806. */
  807. void *omap_gem_vaddr(struct drm_gem_object *obj)
  808. {
  809. struct omap_gem_object *omap_obj = to_omap_bo(obj);
  810. WARN_ON(!mutex_is_locked(&obj->dev->struct_mutex));
  811. if (!omap_obj->vaddr) {
  812. struct page **pages;
  813. int ret = get_pages(obj, &pages);
  814. if (ret)
  815. return ERR_PTR(ret);
  816. omap_obj->vaddr = vmap(pages, obj->size >> PAGE_SHIFT,
  817. VM_MAP, pgprot_writecombine(PAGE_KERNEL));
  818. }
  819. return omap_obj->vaddr;
  820. }
  821. #ifdef CONFIG_PM
  822. /* re-pin objects in DMM in resume path: */
  823. int omap_gem_resume(struct device *dev)
  824. {
  825. struct drm_device *drm_dev = dev_get_drvdata(dev);
  826. struct omap_drm_private *priv = drm_dev->dev_private;
  827. struct omap_gem_object *omap_obj;
  828. int ret = 0;
  829. list_for_each_entry(omap_obj, &priv->obj_list, mm_list) {
  830. if (omap_obj->block) {
  831. struct drm_gem_object *obj = &omap_obj->base;
  832. uint32_t npages = obj->size >> PAGE_SHIFT;
  833. WARN_ON(!omap_obj->pages); /* this can't happen */
  834. ret = tiler_pin(omap_obj->block,
  835. omap_obj->pages, npages,
  836. omap_obj->roll, true);
  837. if (ret) {
  838. dev_err(dev, "could not repin: %d\n", ret);
  839. return ret;
  840. }
  841. }
  842. }
  843. return 0;
  844. }
  845. #endif
  846. #ifdef CONFIG_DEBUG_FS
  847. void omap_gem_describe(struct drm_gem_object *obj, struct seq_file *m)
  848. {
  849. struct drm_device *dev = obj->dev;
  850. struct omap_gem_object *omap_obj = to_omap_bo(obj);
  851. uint64_t off = 0;
  852. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  853. if (obj->map_list.map)
  854. off = (uint64_t)obj->map_list.hash.key;
  855. seq_printf(m, "%08x: %2d (%2d) %08llx %08Zx (%2d) %p %4d",
  856. omap_obj->flags, obj->name, obj->refcount.refcount.counter,
  857. off, omap_obj->paddr, omap_obj->paddr_cnt,
  858. omap_obj->vaddr, omap_obj->roll);
  859. if (omap_obj->flags & OMAP_BO_TILED) {
  860. seq_printf(m, " %dx%d", omap_obj->width, omap_obj->height);
  861. if (omap_obj->block) {
  862. struct tcm_area *area = &omap_obj->block->area;
  863. seq_printf(m, " (%dx%d, %dx%d)",
  864. area->p0.x, area->p0.y,
  865. area->p1.x, area->p1.y);
  866. }
  867. } else {
  868. seq_printf(m, " %d", obj->size);
  869. }
  870. seq_printf(m, "\n");
  871. }
  872. void omap_gem_describe_objects(struct list_head *list, struct seq_file *m)
  873. {
  874. struct omap_gem_object *omap_obj;
  875. int count = 0;
  876. size_t size = 0;
  877. list_for_each_entry(omap_obj, list, mm_list) {
  878. struct drm_gem_object *obj = &omap_obj->base;
  879. seq_printf(m, " ");
  880. omap_gem_describe(obj, m);
  881. count++;
  882. size += obj->size;
  883. }
  884. seq_printf(m, "Total %d objects, %zu bytes\n", count, size);
  885. }
  886. #endif
  887. /* Buffer Synchronization:
  888. */
  889. struct omap_gem_sync_waiter {
  890. struct list_head list;
  891. struct omap_gem_object *omap_obj;
  892. enum omap_gem_op op;
  893. uint32_t read_target, write_target;
  894. /* notify called w/ sync_lock held */
  895. void (*notify)(void *arg);
  896. void *arg;
  897. };
  898. /* list of omap_gem_sync_waiter.. the notify fxn gets called back when
  899. * the read and/or write target count is achieved which can call a user
  900. * callback (ex. to kick 3d and/or 2d), wakeup blocked task (prep for
  901. * cpu access), etc.
  902. */
  903. static LIST_HEAD(waiters);
  904. static inline bool is_waiting(struct omap_gem_sync_waiter *waiter)
  905. {
  906. struct omap_gem_object *omap_obj = waiter->omap_obj;
  907. if ((waiter->op & OMAP_GEM_READ) &&
  908. (omap_obj->sync->read_complete < waiter->read_target))
  909. return true;
  910. if ((waiter->op & OMAP_GEM_WRITE) &&
  911. (omap_obj->sync->write_complete < waiter->write_target))
  912. return true;
  913. return false;
  914. }
  915. /* macro for sync debug.. */
  916. #define SYNCDBG 0
  917. #define SYNC(fmt, ...) do { if (SYNCDBG) \
  918. printk(KERN_ERR "%s:%d: "fmt"\n", \
  919. __func__, __LINE__, ##__VA_ARGS__); \
  920. } while (0)
  921. static void sync_op_update(void)
  922. {
  923. struct omap_gem_sync_waiter *waiter, *n;
  924. list_for_each_entry_safe(waiter, n, &waiters, list) {
  925. if (!is_waiting(waiter)) {
  926. list_del(&waiter->list);
  927. SYNC("notify: %p", waiter);
  928. waiter->notify(waiter->arg);
  929. kfree(waiter);
  930. }
  931. }
  932. }
  933. static inline int sync_op(struct drm_gem_object *obj,
  934. enum omap_gem_op op, bool start)
  935. {
  936. struct omap_gem_object *omap_obj = to_omap_bo(obj);
  937. int ret = 0;
  938. spin_lock(&sync_lock);
  939. if (!omap_obj->sync) {
  940. omap_obj->sync = kzalloc(sizeof(*omap_obj->sync), GFP_ATOMIC);
  941. if (!omap_obj->sync) {
  942. ret = -ENOMEM;
  943. goto unlock;
  944. }
  945. }
  946. if (start) {
  947. if (op & OMAP_GEM_READ)
  948. omap_obj->sync->read_pending++;
  949. if (op & OMAP_GEM_WRITE)
  950. omap_obj->sync->write_pending++;
  951. } else {
  952. if (op & OMAP_GEM_READ)
  953. omap_obj->sync->read_complete++;
  954. if (op & OMAP_GEM_WRITE)
  955. omap_obj->sync->write_complete++;
  956. sync_op_update();
  957. }
  958. unlock:
  959. spin_unlock(&sync_lock);
  960. return ret;
  961. }
  962. /* it is a bit lame to handle updates in this sort of polling way, but
  963. * in case of PVR, the GPU can directly update read/write complete
  964. * values, and not really tell us which ones it updated.. this also
  965. * means that sync_lock is not quite sufficient. So we'll need to
  966. * do something a bit better when it comes time to add support for
  967. * separate 2d hw..
  968. */
  969. void omap_gem_op_update(void)
  970. {
  971. spin_lock(&sync_lock);
  972. sync_op_update();
  973. spin_unlock(&sync_lock);
  974. }
  975. /* mark the start of read and/or write operation */
  976. int omap_gem_op_start(struct drm_gem_object *obj, enum omap_gem_op op)
  977. {
  978. return sync_op(obj, op, true);
  979. }
  980. int omap_gem_op_finish(struct drm_gem_object *obj, enum omap_gem_op op)
  981. {
  982. return sync_op(obj, op, false);
  983. }
  984. static DECLARE_WAIT_QUEUE_HEAD(sync_event);
  985. static void sync_notify(void *arg)
  986. {
  987. struct task_struct **waiter_task = arg;
  988. *waiter_task = NULL;
  989. wake_up_all(&sync_event);
  990. }
  991. int omap_gem_op_sync(struct drm_gem_object *obj, enum omap_gem_op op)
  992. {
  993. struct omap_gem_object *omap_obj = to_omap_bo(obj);
  994. int ret = 0;
  995. if (omap_obj->sync) {
  996. struct task_struct *waiter_task = current;
  997. struct omap_gem_sync_waiter *waiter =
  998. kzalloc(sizeof(*waiter), GFP_KERNEL);
  999. if (!waiter)
  1000. return -ENOMEM;
  1001. waiter->omap_obj = omap_obj;
  1002. waiter->op = op;
  1003. waiter->read_target = omap_obj->sync->read_pending;
  1004. waiter->write_target = omap_obj->sync->write_pending;
  1005. waiter->notify = sync_notify;
  1006. waiter->arg = &waiter_task;
  1007. spin_lock(&sync_lock);
  1008. if (is_waiting(waiter)) {
  1009. SYNC("waited: %p", waiter);
  1010. list_add_tail(&waiter->list, &waiters);
  1011. spin_unlock(&sync_lock);
  1012. ret = wait_event_interruptible(sync_event,
  1013. (waiter_task == NULL));
  1014. spin_lock(&sync_lock);
  1015. if (waiter_task) {
  1016. SYNC("interrupted: %p", waiter);
  1017. /* we were interrupted */
  1018. list_del(&waiter->list);
  1019. waiter_task = NULL;
  1020. } else {
  1021. /* freed in sync_op_update() */
  1022. waiter = NULL;
  1023. }
  1024. }
  1025. spin_unlock(&sync_lock);
  1026. if (waiter)
  1027. kfree(waiter);
  1028. }
  1029. return ret;
  1030. }
  1031. /* call fxn(arg), either synchronously or asynchronously if the op
  1032. * is currently blocked.. fxn() can be called from any context
  1033. *
  1034. * (TODO for now fxn is called back from whichever context calls
  1035. * omap_gem_op_update().. but this could be better defined later
  1036. * if needed)
  1037. *
  1038. * TODO more code in common w/ _sync()..
  1039. */
  1040. int omap_gem_op_async(struct drm_gem_object *obj, enum omap_gem_op op,
  1041. void (*fxn)(void *arg), void *arg)
  1042. {
  1043. struct omap_gem_object *omap_obj = to_omap_bo(obj);
  1044. if (omap_obj->sync) {
  1045. struct omap_gem_sync_waiter *waiter =
  1046. kzalloc(sizeof(*waiter), GFP_ATOMIC);
  1047. if (!waiter)
  1048. return -ENOMEM;
  1049. waiter->omap_obj = omap_obj;
  1050. waiter->op = op;
  1051. waiter->read_target = omap_obj->sync->read_pending;
  1052. waiter->write_target = omap_obj->sync->write_pending;
  1053. waiter->notify = fxn;
  1054. waiter->arg = arg;
  1055. spin_lock(&sync_lock);
  1056. if (is_waiting(waiter)) {
  1057. SYNC("waited: %p", waiter);
  1058. list_add_tail(&waiter->list, &waiters);
  1059. spin_unlock(&sync_lock);
  1060. return 0;
  1061. }
  1062. spin_unlock(&sync_lock);
  1063. }
  1064. /* no waiting.. */
  1065. fxn(arg);
  1066. return 0;
  1067. }
  1068. /* special API so PVR can update the buffer to use a sync-object allocated
  1069. * from it's sync-obj heap. Only used for a newly allocated (from PVR's
  1070. * perspective) sync-object, so we overwrite the new syncobj w/ values
  1071. * from the already allocated syncobj (if there is one)
  1072. */
  1073. int omap_gem_set_sync_object(struct drm_gem_object *obj, void *syncobj)
  1074. {
  1075. struct omap_gem_object *omap_obj = to_omap_bo(obj);
  1076. int ret = 0;
  1077. spin_lock(&sync_lock);
  1078. if ((omap_obj->flags & OMAP_BO_EXT_SYNC) && !syncobj) {
  1079. /* clearing a previously set syncobj */
  1080. syncobj = kmemdup(omap_obj->sync, sizeof(*omap_obj->sync),
  1081. GFP_ATOMIC);
  1082. if (!syncobj) {
  1083. ret = -ENOMEM;
  1084. goto unlock;
  1085. }
  1086. omap_obj->flags &= ~OMAP_BO_EXT_SYNC;
  1087. omap_obj->sync = syncobj;
  1088. } else if (syncobj && !(omap_obj->flags & OMAP_BO_EXT_SYNC)) {
  1089. /* replacing an existing syncobj */
  1090. if (omap_obj->sync) {
  1091. memcpy(syncobj, omap_obj->sync, sizeof(*omap_obj->sync));
  1092. kfree(omap_obj->sync);
  1093. }
  1094. omap_obj->flags |= OMAP_BO_EXT_SYNC;
  1095. omap_obj->sync = syncobj;
  1096. }
  1097. unlock:
  1098. spin_unlock(&sync_lock);
  1099. return ret;
  1100. }
  1101. int omap_gem_init_object(struct drm_gem_object *obj)
  1102. {
  1103. return -EINVAL; /* unused */
  1104. }
  1105. /* don't call directly.. called from GEM core when it is time to actually
  1106. * free the object..
  1107. */
  1108. void omap_gem_free_object(struct drm_gem_object *obj)
  1109. {
  1110. struct drm_device *dev = obj->dev;
  1111. struct omap_gem_object *omap_obj = to_omap_bo(obj);
  1112. evict(obj);
  1113. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  1114. list_del(&omap_obj->mm_list);
  1115. if (obj->map_list.map)
  1116. drm_gem_free_mmap_offset(obj);
  1117. /* this means the object is still pinned.. which really should
  1118. * not happen. I think..
  1119. */
  1120. WARN_ON(omap_obj->paddr_cnt > 0);
  1121. /* don't free externally allocated backing memory */
  1122. if (!(omap_obj->flags & OMAP_BO_EXT_MEM)) {
  1123. if (omap_obj->pages)
  1124. omap_gem_detach_pages(obj);
  1125. if (!is_shmem(obj)) {
  1126. dma_free_writecombine(dev->dev, obj->size,
  1127. omap_obj->vaddr, omap_obj->paddr);
  1128. } else if (omap_obj->vaddr) {
  1129. vunmap(omap_obj->vaddr);
  1130. }
  1131. }
  1132. /* don't free externally allocated syncobj */
  1133. if (!(omap_obj->flags & OMAP_BO_EXT_SYNC))
  1134. kfree(omap_obj->sync);
  1135. drm_gem_object_release(obj);
  1136. kfree(obj);
  1137. }
  1138. /* convenience method to construct a GEM buffer object, and userspace handle */
  1139. int omap_gem_new_handle(struct drm_device *dev, struct drm_file *file,
  1140. union omap_gem_size gsize, uint32_t flags, uint32_t *handle)
  1141. {
  1142. struct drm_gem_object *obj;
  1143. int ret;
  1144. obj = omap_gem_new(dev, gsize, flags);
  1145. if (!obj)
  1146. return -ENOMEM;
  1147. ret = drm_gem_handle_create(file, obj, handle);
  1148. if (ret) {
  1149. drm_gem_object_release(obj);
  1150. kfree(obj); /* TODO isn't there a dtor to call? just copying i915 */
  1151. return ret;
  1152. }
  1153. /* drop reference from allocate - handle holds it now */
  1154. drm_gem_object_unreference_unlocked(obj);
  1155. return 0;
  1156. }
  1157. /* GEM buffer object constructor */
  1158. struct drm_gem_object *omap_gem_new(struct drm_device *dev,
  1159. union omap_gem_size gsize, uint32_t flags)
  1160. {
  1161. struct omap_drm_private *priv = dev->dev_private;
  1162. struct omap_gem_object *omap_obj;
  1163. struct drm_gem_object *obj = NULL;
  1164. size_t size;
  1165. int ret;
  1166. if (flags & OMAP_BO_TILED) {
  1167. if (!usergart) {
  1168. dev_err(dev->dev, "Tiled buffers require DMM\n");
  1169. goto fail;
  1170. }
  1171. /* tiled buffers are always shmem paged backed.. when they are
  1172. * scanned out, they are remapped into DMM/TILER
  1173. */
  1174. flags &= ~OMAP_BO_SCANOUT;
  1175. /* currently don't allow cached buffers.. there is some caching
  1176. * stuff that needs to be handled better
  1177. */
  1178. flags &= ~(OMAP_BO_CACHED|OMAP_BO_UNCACHED);
  1179. flags |= OMAP_BO_WC;
  1180. /* align dimensions to slot boundaries... */
  1181. tiler_align(gem2fmt(flags),
  1182. &gsize.tiled.width, &gsize.tiled.height);
  1183. /* ...and calculate size based on aligned dimensions */
  1184. size = tiler_size(gem2fmt(flags),
  1185. gsize.tiled.width, gsize.tiled.height);
  1186. } else {
  1187. size = PAGE_ALIGN(gsize.bytes);
  1188. }
  1189. omap_obj = kzalloc(sizeof(*omap_obj), GFP_KERNEL);
  1190. if (!omap_obj)
  1191. goto fail;
  1192. list_add(&omap_obj->mm_list, &priv->obj_list);
  1193. obj = &omap_obj->base;
  1194. if ((flags & OMAP_BO_SCANOUT) && !priv->has_dmm) {
  1195. /* attempt to allocate contiguous memory if we don't
  1196. * have DMM for remappign discontiguous buffers
  1197. */
  1198. omap_obj->vaddr = dma_alloc_writecombine(dev->dev, size,
  1199. &omap_obj->paddr, GFP_KERNEL);
  1200. if (omap_obj->vaddr)
  1201. flags |= OMAP_BO_DMA;
  1202. }
  1203. omap_obj->flags = flags;
  1204. if (flags & OMAP_BO_TILED) {
  1205. omap_obj->width = gsize.tiled.width;
  1206. omap_obj->height = gsize.tiled.height;
  1207. }
  1208. if (flags & (OMAP_BO_DMA|OMAP_BO_EXT_MEM))
  1209. ret = drm_gem_private_object_init(dev, obj, size);
  1210. else
  1211. ret = drm_gem_object_init(dev, obj, size);
  1212. if (ret)
  1213. goto fail;
  1214. return obj;
  1215. fail:
  1216. if (obj)
  1217. omap_gem_free_object(obj);
  1218. return NULL;
  1219. }
  1220. /* init/cleanup.. if DMM is used, we need to set some stuff up.. */
  1221. void omap_gem_init(struct drm_device *dev)
  1222. {
  1223. struct omap_drm_private *priv = dev->dev_private;
  1224. const enum tiler_fmt fmts[] = {
  1225. TILFMT_8BIT, TILFMT_16BIT, TILFMT_32BIT
  1226. };
  1227. int i, j;
  1228. if (!dmm_is_available()) {
  1229. /* DMM only supported on OMAP4 and later, so this isn't fatal */
  1230. dev_warn(dev->dev, "DMM not available, disable DMM support\n");
  1231. return;
  1232. }
  1233. usergart = kcalloc(3, sizeof(*usergart), GFP_KERNEL);
  1234. if (!usergart)
  1235. return;
  1236. /* reserve 4k aligned/wide regions for userspace mappings: */
  1237. for (i = 0; i < ARRAY_SIZE(fmts); i++) {
  1238. uint16_t h = 1, w = PAGE_SIZE >> i;
  1239. tiler_align(fmts[i], &w, &h);
  1240. /* note: since each region is 1 4kb page wide, and minimum
  1241. * number of rows, the height ends up being the same as the
  1242. * # of pages in the region
  1243. */
  1244. usergart[i].height = h;
  1245. usergart[i].height_shift = ilog2(h);
  1246. usergart[i].stride_pfn = tiler_stride(fmts[i], 0) >> PAGE_SHIFT;
  1247. usergart[i].slot_shift = ilog2((PAGE_SIZE / h) >> i);
  1248. for (j = 0; j < NUM_USERGART_ENTRIES; j++) {
  1249. struct usergart_entry *entry = &usergart[i].entry[j];
  1250. struct tiler_block *block =
  1251. tiler_reserve_2d(fmts[i], w, h,
  1252. PAGE_SIZE);
  1253. if (IS_ERR(block)) {
  1254. dev_err(dev->dev,
  1255. "reserve failed: %d, %d, %ld\n",
  1256. i, j, PTR_ERR(block));
  1257. return;
  1258. }
  1259. entry->paddr = tiler_ssptr(block);
  1260. entry->block = block;
  1261. DBG("%d:%d: %dx%d: paddr=%08x stride=%d", i, j, w, h,
  1262. entry->paddr,
  1263. usergart[i].stride_pfn << PAGE_SHIFT);
  1264. }
  1265. }
  1266. priv->has_dmm = true;
  1267. }
  1268. void omap_gem_deinit(struct drm_device *dev)
  1269. {
  1270. /* I believe we can rely on there being no more outstanding GEM
  1271. * objects which could depend on usergart/dmm at this point.
  1272. */
  1273. kfree(usergart);
  1274. }