omap_gem.c 43 KB

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