omap_gem.c 39 KB

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