omap_gem.c 41 KB

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