omap_gem.c 39 KB

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