omap_gem.c 39 KB

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