i915_gem.c 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516
  1. /*
  2. * Copyright © 2008 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. * IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Eric Anholt <eric@anholt.net>
  25. *
  26. */
  27. #include "drmP.h"
  28. #include "drm.h"
  29. #include "i915_drm.h"
  30. #include "i915_drv.h"
  31. #include <linux/swap.h>
  32. static int
  33. i915_gem_object_set_domain(struct drm_gem_object *obj,
  34. uint32_t read_domains,
  35. uint32_t write_domain);
  36. static int
  37. i915_gem_object_set_domain_range(struct drm_gem_object *obj,
  38. uint64_t offset,
  39. uint64_t size,
  40. uint32_t read_domains,
  41. uint32_t write_domain);
  42. static int
  43. i915_gem_set_domain(struct drm_gem_object *obj,
  44. struct drm_file *file_priv,
  45. uint32_t read_domains,
  46. uint32_t write_domain);
  47. static int i915_gem_object_get_page_list(struct drm_gem_object *obj);
  48. static void i915_gem_object_free_page_list(struct drm_gem_object *obj);
  49. static int i915_gem_object_wait_rendering(struct drm_gem_object *obj);
  50. int
  51. i915_gem_init_ioctl(struct drm_device *dev, void *data,
  52. struct drm_file *file_priv)
  53. {
  54. drm_i915_private_t *dev_priv = dev->dev_private;
  55. struct drm_i915_gem_init *args = data;
  56. mutex_lock(&dev->struct_mutex);
  57. if (args->gtt_start >= args->gtt_end ||
  58. (args->gtt_start & (PAGE_SIZE - 1)) != 0 ||
  59. (args->gtt_end & (PAGE_SIZE - 1)) != 0) {
  60. mutex_unlock(&dev->struct_mutex);
  61. return -EINVAL;
  62. }
  63. drm_mm_init(&dev_priv->mm.gtt_space, args->gtt_start,
  64. args->gtt_end - args->gtt_start);
  65. dev->gtt_total = (uint32_t) (args->gtt_end - args->gtt_start);
  66. mutex_unlock(&dev->struct_mutex);
  67. return 0;
  68. }
  69. /**
  70. * Creates a new mm object and returns a handle to it.
  71. */
  72. int
  73. i915_gem_create_ioctl(struct drm_device *dev, void *data,
  74. struct drm_file *file_priv)
  75. {
  76. struct drm_i915_gem_create *args = data;
  77. struct drm_gem_object *obj;
  78. int handle, ret;
  79. args->size = roundup(args->size, PAGE_SIZE);
  80. /* Allocate the new object */
  81. obj = drm_gem_object_alloc(dev, args->size);
  82. if (obj == NULL)
  83. return -ENOMEM;
  84. ret = drm_gem_handle_create(file_priv, obj, &handle);
  85. mutex_lock(&dev->struct_mutex);
  86. drm_gem_object_handle_unreference(obj);
  87. mutex_unlock(&dev->struct_mutex);
  88. if (ret)
  89. return ret;
  90. args->handle = handle;
  91. return 0;
  92. }
  93. /**
  94. * Reads data from the object referenced by handle.
  95. *
  96. * On error, the contents of *data are undefined.
  97. */
  98. int
  99. i915_gem_pread_ioctl(struct drm_device *dev, void *data,
  100. struct drm_file *file_priv)
  101. {
  102. struct drm_i915_gem_pread *args = data;
  103. struct drm_gem_object *obj;
  104. struct drm_i915_gem_object *obj_priv;
  105. ssize_t read;
  106. loff_t offset;
  107. int ret;
  108. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  109. if (obj == NULL)
  110. return -EBADF;
  111. obj_priv = obj->driver_private;
  112. /* Bounds check source.
  113. *
  114. * XXX: This could use review for overflow issues...
  115. */
  116. if (args->offset > obj->size || args->size > obj->size ||
  117. args->offset + args->size > obj->size) {
  118. drm_gem_object_unreference(obj);
  119. return -EINVAL;
  120. }
  121. mutex_lock(&dev->struct_mutex);
  122. ret = i915_gem_object_set_domain_range(obj, args->offset, args->size,
  123. I915_GEM_DOMAIN_CPU, 0);
  124. if (ret != 0) {
  125. drm_gem_object_unreference(obj);
  126. mutex_unlock(&dev->struct_mutex);
  127. }
  128. offset = args->offset;
  129. read = vfs_read(obj->filp, (char __user *)(uintptr_t)args->data_ptr,
  130. args->size, &offset);
  131. if (read != args->size) {
  132. drm_gem_object_unreference(obj);
  133. mutex_unlock(&dev->struct_mutex);
  134. if (read < 0)
  135. return read;
  136. else
  137. return -EINVAL;
  138. }
  139. drm_gem_object_unreference(obj);
  140. mutex_unlock(&dev->struct_mutex);
  141. return 0;
  142. }
  143. static int
  144. i915_gem_gtt_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
  145. struct drm_i915_gem_pwrite *args,
  146. struct drm_file *file_priv)
  147. {
  148. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  149. ssize_t remain;
  150. loff_t offset;
  151. char __user *user_data;
  152. char __iomem *vaddr;
  153. char *vaddr_atomic;
  154. int i, o, l;
  155. int ret = 0;
  156. unsigned long pfn;
  157. unsigned long unwritten;
  158. user_data = (char __user *) (uintptr_t) args->data_ptr;
  159. remain = args->size;
  160. if (!access_ok(VERIFY_READ, user_data, remain))
  161. return -EFAULT;
  162. mutex_lock(&dev->struct_mutex);
  163. ret = i915_gem_object_pin(obj, 0);
  164. if (ret) {
  165. mutex_unlock(&dev->struct_mutex);
  166. return ret;
  167. }
  168. ret = i915_gem_set_domain(obj, file_priv,
  169. I915_GEM_DOMAIN_GTT, I915_GEM_DOMAIN_GTT);
  170. if (ret)
  171. goto fail;
  172. obj_priv = obj->driver_private;
  173. offset = obj_priv->gtt_offset + args->offset;
  174. obj_priv->dirty = 1;
  175. while (remain > 0) {
  176. /* Operation in this page
  177. *
  178. * i = page number
  179. * o = offset within page
  180. * l = bytes to copy
  181. */
  182. i = offset >> PAGE_SHIFT;
  183. o = offset & (PAGE_SIZE-1);
  184. l = remain;
  185. if ((o + l) > PAGE_SIZE)
  186. l = PAGE_SIZE - o;
  187. pfn = (dev->agp->base >> PAGE_SHIFT) + i;
  188. #ifdef CONFIG_HIGHMEM
  189. /* This is a workaround for the low performance of iounmap
  190. * (approximate 10% cpu cost on normal 3D workloads).
  191. * kmap_atomic on HIGHMEM kernels happens to let us map card
  192. * memory without taking IPIs. When the vmap rework lands
  193. * we should be able to dump this hack.
  194. */
  195. vaddr_atomic = kmap_atomic_pfn(pfn, KM_USER0);
  196. #if WATCH_PWRITE
  197. DRM_INFO("pwrite i %d o %d l %d pfn %ld vaddr %p\n",
  198. i, o, l, pfn, vaddr_atomic);
  199. #endif
  200. unwritten = __copy_from_user_inatomic_nocache(vaddr_atomic + o,
  201. user_data, l);
  202. kunmap_atomic(vaddr_atomic, KM_USER0);
  203. if (unwritten)
  204. #endif /* CONFIG_HIGHMEM */
  205. {
  206. vaddr = ioremap_wc(pfn << PAGE_SHIFT, PAGE_SIZE);
  207. #if WATCH_PWRITE
  208. DRM_INFO("pwrite slow i %d o %d l %d "
  209. "pfn %ld vaddr %p\n",
  210. i, o, l, pfn, vaddr);
  211. #endif
  212. if (vaddr == NULL) {
  213. ret = -EFAULT;
  214. goto fail;
  215. }
  216. unwritten = __copy_from_user(vaddr + o, user_data, l);
  217. #if WATCH_PWRITE
  218. DRM_INFO("unwritten %ld\n", unwritten);
  219. #endif
  220. iounmap(vaddr);
  221. if (unwritten) {
  222. ret = -EFAULT;
  223. goto fail;
  224. }
  225. }
  226. remain -= l;
  227. user_data += l;
  228. offset += l;
  229. }
  230. #if WATCH_PWRITE && 1
  231. i915_gem_clflush_object(obj);
  232. i915_gem_dump_object(obj, args->offset + args->size, __func__, ~0);
  233. i915_gem_clflush_object(obj);
  234. #endif
  235. fail:
  236. i915_gem_object_unpin(obj);
  237. mutex_unlock(&dev->struct_mutex);
  238. return ret;
  239. }
  240. static int
  241. i915_gem_shmem_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
  242. struct drm_i915_gem_pwrite *args,
  243. struct drm_file *file_priv)
  244. {
  245. int ret;
  246. loff_t offset;
  247. ssize_t written;
  248. mutex_lock(&dev->struct_mutex);
  249. ret = i915_gem_set_domain(obj, file_priv,
  250. I915_GEM_DOMAIN_CPU, I915_GEM_DOMAIN_CPU);
  251. if (ret) {
  252. mutex_unlock(&dev->struct_mutex);
  253. return ret;
  254. }
  255. offset = args->offset;
  256. written = vfs_write(obj->filp,
  257. (char __user *)(uintptr_t) args->data_ptr,
  258. args->size, &offset);
  259. if (written != args->size) {
  260. mutex_unlock(&dev->struct_mutex);
  261. if (written < 0)
  262. return written;
  263. else
  264. return -EINVAL;
  265. }
  266. mutex_unlock(&dev->struct_mutex);
  267. return 0;
  268. }
  269. /**
  270. * Writes data to the object referenced by handle.
  271. *
  272. * On error, the contents of the buffer that were to be modified are undefined.
  273. */
  274. int
  275. i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
  276. struct drm_file *file_priv)
  277. {
  278. struct drm_i915_gem_pwrite *args = data;
  279. struct drm_gem_object *obj;
  280. struct drm_i915_gem_object *obj_priv;
  281. int ret = 0;
  282. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  283. if (obj == NULL)
  284. return -EBADF;
  285. obj_priv = obj->driver_private;
  286. /* Bounds check destination.
  287. *
  288. * XXX: This could use review for overflow issues...
  289. */
  290. if (args->offset > obj->size || args->size > obj->size ||
  291. args->offset + args->size > obj->size) {
  292. drm_gem_object_unreference(obj);
  293. return -EINVAL;
  294. }
  295. /* We can only do the GTT pwrite on untiled buffers, as otherwise
  296. * it would end up going through the fenced access, and we'll get
  297. * different detiling behavior between reading and writing.
  298. * pread/pwrite currently are reading and writing from the CPU
  299. * perspective, requiring manual detiling by the client.
  300. */
  301. if (obj_priv->tiling_mode == I915_TILING_NONE &&
  302. dev->gtt_total != 0)
  303. ret = i915_gem_gtt_pwrite(dev, obj, args, file_priv);
  304. else
  305. ret = i915_gem_shmem_pwrite(dev, obj, args, file_priv);
  306. #if WATCH_PWRITE
  307. if (ret)
  308. DRM_INFO("pwrite failed %d\n", ret);
  309. #endif
  310. drm_gem_object_unreference(obj);
  311. return ret;
  312. }
  313. /**
  314. * Called when user space prepares to use an object
  315. */
  316. int
  317. i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
  318. struct drm_file *file_priv)
  319. {
  320. struct drm_i915_gem_set_domain *args = data;
  321. struct drm_gem_object *obj;
  322. int ret;
  323. if (!(dev->driver->driver_features & DRIVER_GEM))
  324. return -ENODEV;
  325. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  326. if (obj == NULL)
  327. return -EBADF;
  328. mutex_lock(&dev->struct_mutex);
  329. #if WATCH_BUF
  330. DRM_INFO("set_domain_ioctl %p(%d), %08x %08x\n",
  331. obj, obj->size, args->read_domains, args->write_domain);
  332. #endif
  333. ret = i915_gem_set_domain(obj, file_priv,
  334. args->read_domains, args->write_domain);
  335. drm_gem_object_unreference(obj);
  336. mutex_unlock(&dev->struct_mutex);
  337. return ret;
  338. }
  339. /**
  340. * Called when user space has done writes to this buffer
  341. */
  342. int
  343. i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
  344. struct drm_file *file_priv)
  345. {
  346. struct drm_i915_gem_sw_finish *args = data;
  347. struct drm_gem_object *obj;
  348. struct drm_i915_gem_object *obj_priv;
  349. int ret = 0;
  350. if (!(dev->driver->driver_features & DRIVER_GEM))
  351. return -ENODEV;
  352. mutex_lock(&dev->struct_mutex);
  353. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  354. if (obj == NULL) {
  355. mutex_unlock(&dev->struct_mutex);
  356. return -EBADF;
  357. }
  358. #if WATCH_BUF
  359. DRM_INFO("%s: sw_finish %d (%p %d)\n",
  360. __func__, args->handle, obj, obj->size);
  361. #endif
  362. obj_priv = obj->driver_private;
  363. /* Pinned buffers may be scanout, so flush the cache */
  364. if ((obj->write_domain & I915_GEM_DOMAIN_CPU) && obj_priv->pin_count) {
  365. i915_gem_clflush_object(obj);
  366. drm_agp_chipset_flush(dev);
  367. }
  368. drm_gem_object_unreference(obj);
  369. mutex_unlock(&dev->struct_mutex);
  370. return ret;
  371. }
  372. /**
  373. * Maps the contents of an object, returning the address it is mapped
  374. * into.
  375. *
  376. * While the mapping holds a reference on the contents of the object, it doesn't
  377. * imply a ref on the object itself.
  378. */
  379. int
  380. i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
  381. struct drm_file *file_priv)
  382. {
  383. struct drm_i915_gem_mmap *args = data;
  384. struct drm_gem_object *obj;
  385. loff_t offset;
  386. unsigned long addr;
  387. if (!(dev->driver->driver_features & DRIVER_GEM))
  388. return -ENODEV;
  389. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  390. if (obj == NULL)
  391. return -EBADF;
  392. offset = args->offset;
  393. down_write(&current->mm->mmap_sem);
  394. addr = do_mmap(obj->filp, 0, args->size,
  395. PROT_READ | PROT_WRITE, MAP_SHARED,
  396. args->offset);
  397. up_write(&current->mm->mmap_sem);
  398. mutex_lock(&dev->struct_mutex);
  399. drm_gem_object_unreference(obj);
  400. mutex_unlock(&dev->struct_mutex);
  401. if (IS_ERR((void *)addr))
  402. return addr;
  403. args->addr_ptr = (uint64_t) addr;
  404. return 0;
  405. }
  406. static void
  407. i915_gem_object_free_page_list(struct drm_gem_object *obj)
  408. {
  409. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  410. int page_count = obj->size / PAGE_SIZE;
  411. int i;
  412. if (obj_priv->page_list == NULL)
  413. return;
  414. for (i = 0; i < page_count; i++)
  415. if (obj_priv->page_list[i] != NULL) {
  416. if (obj_priv->dirty)
  417. set_page_dirty(obj_priv->page_list[i]);
  418. mark_page_accessed(obj_priv->page_list[i]);
  419. page_cache_release(obj_priv->page_list[i]);
  420. }
  421. obj_priv->dirty = 0;
  422. drm_free(obj_priv->page_list,
  423. page_count * sizeof(struct page *),
  424. DRM_MEM_DRIVER);
  425. obj_priv->page_list = NULL;
  426. }
  427. static void
  428. i915_gem_object_move_to_active(struct drm_gem_object *obj)
  429. {
  430. struct drm_device *dev = obj->dev;
  431. drm_i915_private_t *dev_priv = dev->dev_private;
  432. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  433. /* Add a reference if we're newly entering the active list. */
  434. if (!obj_priv->active) {
  435. drm_gem_object_reference(obj);
  436. obj_priv->active = 1;
  437. }
  438. /* Move from whatever list we were on to the tail of execution. */
  439. list_move_tail(&obj_priv->list,
  440. &dev_priv->mm.active_list);
  441. }
  442. static void
  443. i915_gem_object_move_to_inactive(struct drm_gem_object *obj)
  444. {
  445. struct drm_device *dev = obj->dev;
  446. drm_i915_private_t *dev_priv = dev->dev_private;
  447. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  448. i915_verify_inactive(dev, __FILE__, __LINE__);
  449. if (obj_priv->pin_count != 0)
  450. list_del_init(&obj_priv->list);
  451. else
  452. list_move_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
  453. if (obj_priv->active) {
  454. obj_priv->active = 0;
  455. drm_gem_object_unreference(obj);
  456. }
  457. i915_verify_inactive(dev, __FILE__, __LINE__);
  458. }
  459. /**
  460. * Creates a new sequence number, emitting a write of it to the status page
  461. * plus an interrupt, which will trigger i915_user_interrupt_handler.
  462. *
  463. * Must be called with struct_lock held.
  464. *
  465. * Returned sequence numbers are nonzero on success.
  466. */
  467. static uint32_t
  468. i915_add_request(struct drm_device *dev, uint32_t flush_domains)
  469. {
  470. drm_i915_private_t *dev_priv = dev->dev_private;
  471. struct drm_i915_gem_request *request;
  472. uint32_t seqno;
  473. int was_empty;
  474. RING_LOCALS;
  475. request = drm_calloc(1, sizeof(*request), DRM_MEM_DRIVER);
  476. if (request == NULL)
  477. return 0;
  478. /* Grab the seqno we're going to make this request be, and bump the
  479. * next (skipping 0 so it can be the reserved no-seqno value).
  480. */
  481. seqno = dev_priv->mm.next_gem_seqno;
  482. dev_priv->mm.next_gem_seqno++;
  483. if (dev_priv->mm.next_gem_seqno == 0)
  484. dev_priv->mm.next_gem_seqno++;
  485. BEGIN_LP_RING(4);
  486. OUT_RING(MI_STORE_DWORD_INDEX);
  487. OUT_RING(I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
  488. OUT_RING(seqno);
  489. OUT_RING(MI_USER_INTERRUPT);
  490. ADVANCE_LP_RING();
  491. DRM_DEBUG("%d\n", seqno);
  492. request->seqno = seqno;
  493. request->emitted_jiffies = jiffies;
  494. request->flush_domains = flush_domains;
  495. was_empty = list_empty(&dev_priv->mm.request_list);
  496. list_add_tail(&request->list, &dev_priv->mm.request_list);
  497. if (was_empty)
  498. schedule_delayed_work(&dev_priv->mm.retire_work, HZ);
  499. return seqno;
  500. }
  501. /**
  502. * Command execution barrier
  503. *
  504. * Ensures that all commands in the ring are finished
  505. * before signalling the CPU
  506. */
  507. static uint32_t
  508. i915_retire_commands(struct drm_device *dev)
  509. {
  510. drm_i915_private_t *dev_priv = dev->dev_private;
  511. uint32_t cmd = MI_FLUSH | MI_NO_WRITE_FLUSH;
  512. uint32_t flush_domains = 0;
  513. RING_LOCALS;
  514. /* The sampler always gets flushed on i965 (sigh) */
  515. if (IS_I965G(dev))
  516. flush_domains |= I915_GEM_DOMAIN_SAMPLER;
  517. BEGIN_LP_RING(2);
  518. OUT_RING(cmd);
  519. OUT_RING(0); /* noop */
  520. ADVANCE_LP_RING();
  521. return flush_domains;
  522. }
  523. /**
  524. * Moves buffers associated only with the given active seqno from the active
  525. * to inactive list, potentially freeing them.
  526. */
  527. static void
  528. i915_gem_retire_request(struct drm_device *dev,
  529. struct drm_i915_gem_request *request)
  530. {
  531. drm_i915_private_t *dev_priv = dev->dev_private;
  532. /* Move any buffers on the active list that are no longer referenced
  533. * by the ringbuffer to the flushing/inactive lists as appropriate.
  534. */
  535. while (!list_empty(&dev_priv->mm.active_list)) {
  536. struct drm_gem_object *obj;
  537. struct drm_i915_gem_object *obj_priv;
  538. obj_priv = list_first_entry(&dev_priv->mm.active_list,
  539. struct drm_i915_gem_object,
  540. list);
  541. obj = obj_priv->obj;
  542. /* If the seqno being retired doesn't match the oldest in the
  543. * list, then the oldest in the list must still be newer than
  544. * this seqno.
  545. */
  546. if (obj_priv->last_rendering_seqno != request->seqno)
  547. return;
  548. #if WATCH_LRU
  549. DRM_INFO("%s: retire %d moves to inactive list %p\n",
  550. __func__, request->seqno, obj);
  551. #endif
  552. if (obj->write_domain != 0) {
  553. list_move_tail(&obj_priv->list,
  554. &dev_priv->mm.flushing_list);
  555. } else {
  556. i915_gem_object_move_to_inactive(obj);
  557. }
  558. }
  559. if (request->flush_domains != 0) {
  560. struct drm_i915_gem_object *obj_priv, *next;
  561. /* Clear the write domain and activity from any buffers
  562. * that are just waiting for a flush matching the one retired.
  563. */
  564. list_for_each_entry_safe(obj_priv, next,
  565. &dev_priv->mm.flushing_list, list) {
  566. struct drm_gem_object *obj = obj_priv->obj;
  567. if (obj->write_domain & request->flush_domains) {
  568. obj->write_domain = 0;
  569. i915_gem_object_move_to_inactive(obj);
  570. }
  571. }
  572. }
  573. }
  574. /**
  575. * Returns true if seq1 is later than seq2.
  576. */
  577. static int
  578. i915_seqno_passed(uint32_t seq1, uint32_t seq2)
  579. {
  580. return (int32_t)(seq1 - seq2) >= 0;
  581. }
  582. uint32_t
  583. i915_get_gem_seqno(struct drm_device *dev)
  584. {
  585. drm_i915_private_t *dev_priv = dev->dev_private;
  586. return READ_HWSP(dev_priv, I915_GEM_HWS_INDEX);
  587. }
  588. /**
  589. * This function clears the request list as sequence numbers are passed.
  590. */
  591. void
  592. i915_gem_retire_requests(struct drm_device *dev)
  593. {
  594. drm_i915_private_t *dev_priv = dev->dev_private;
  595. uint32_t seqno;
  596. seqno = i915_get_gem_seqno(dev);
  597. while (!list_empty(&dev_priv->mm.request_list)) {
  598. struct drm_i915_gem_request *request;
  599. uint32_t retiring_seqno;
  600. request = list_first_entry(&dev_priv->mm.request_list,
  601. struct drm_i915_gem_request,
  602. list);
  603. retiring_seqno = request->seqno;
  604. if (i915_seqno_passed(seqno, retiring_seqno) ||
  605. dev_priv->mm.wedged) {
  606. i915_gem_retire_request(dev, request);
  607. list_del(&request->list);
  608. drm_free(request, sizeof(*request), DRM_MEM_DRIVER);
  609. } else
  610. break;
  611. }
  612. }
  613. void
  614. i915_gem_retire_work_handler(struct work_struct *work)
  615. {
  616. drm_i915_private_t *dev_priv;
  617. struct drm_device *dev;
  618. dev_priv = container_of(work, drm_i915_private_t,
  619. mm.retire_work.work);
  620. dev = dev_priv->dev;
  621. mutex_lock(&dev->struct_mutex);
  622. i915_gem_retire_requests(dev);
  623. if (!list_empty(&dev_priv->mm.request_list))
  624. schedule_delayed_work(&dev_priv->mm.retire_work, HZ);
  625. mutex_unlock(&dev->struct_mutex);
  626. }
  627. /**
  628. * Waits for a sequence number to be signaled, and cleans up the
  629. * request and object lists appropriately for that event.
  630. */
  631. static int
  632. i915_wait_request(struct drm_device *dev, uint32_t seqno)
  633. {
  634. drm_i915_private_t *dev_priv = dev->dev_private;
  635. int ret = 0;
  636. BUG_ON(seqno == 0);
  637. if (!i915_seqno_passed(i915_get_gem_seqno(dev), seqno)) {
  638. dev_priv->mm.waiting_gem_seqno = seqno;
  639. i915_user_irq_get(dev);
  640. ret = wait_event_interruptible(dev_priv->irq_queue,
  641. i915_seqno_passed(i915_get_gem_seqno(dev),
  642. seqno) ||
  643. dev_priv->mm.wedged);
  644. i915_user_irq_put(dev);
  645. dev_priv->mm.waiting_gem_seqno = 0;
  646. }
  647. if (dev_priv->mm.wedged)
  648. ret = -EIO;
  649. if (ret && ret != -ERESTARTSYS)
  650. DRM_ERROR("%s returns %d (awaiting %d at %d)\n",
  651. __func__, ret, seqno, i915_get_gem_seqno(dev));
  652. /* Directly dispatch request retiring. While we have the work queue
  653. * to handle this, the waiter on a request often wants an associated
  654. * buffer to have made it to the inactive list, and we would need
  655. * a separate wait queue to handle that.
  656. */
  657. if (ret == 0)
  658. i915_gem_retire_requests(dev);
  659. return ret;
  660. }
  661. static void
  662. i915_gem_flush(struct drm_device *dev,
  663. uint32_t invalidate_domains,
  664. uint32_t flush_domains)
  665. {
  666. drm_i915_private_t *dev_priv = dev->dev_private;
  667. uint32_t cmd;
  668. RING_LOCALS;
  669. #if WATCH_EXEC
  670. DRM_INFO("%s: invalidate %08x flush %08x\n", __func__,
  671. invalidate_domains, flush_domains);
  672. #endif
  673. if (flush_domains & I915_GEM_DOMAIN_CPU)
  674. drm_agp_chipset_flush(dev);
  675. if ((invalidate_domains | flush_domains) & ~(I915_GEM_DOMAIN_CPU |
  676. I915_GEM_DOMAIN_GTT)) {
  677. /*
  678. * read/write caches:
  679. *
  680. * I915_GEM_DOMAIN_RENDER is always invalidated, but is
  681. * only flushed if MI_NO_WRITE_FLUSH is unset. On 965, it is
  682. * also flushed at 2d versus 3d pipeline switches.
  683. *
  684. * read-only caches:
  685. *
  686. * I915_GEM_DOMAIN_SAMPLER is flushed on pre-965 if
  687. * MI_READ_FLUSH is set, and is always flushed on 965.
  688. *
  689. * I915_GEM_DOMAIN_COMMAND may not exist?
  690. *
  691. * I915_GEM_DOMAIN_INSTRUCTION, which exists on 965, is
  692. * invalidated when MI_EXE_FLUSH is set.
  693. *
  694. * I915_GEM_DOMAIN_VERTEX, which exists on 965, is
  695. * invalidated with every MI_FLUSH.
  696. *
  697. * TLBs:
  698. *
  699. * On 965, TLBs associated with I915_GEM_DOMAIN_COMMAND
  700. * and I915_GEM_DOMAIN_CPU in are invalidated at PTE write and
  701. * I915_GEM_DOMAIN_RENDER and I915_GEM_DOMAIN_SAMPLER
  702. * are flushed at any MI_FLUSH.
  703. */
  704. cmd = MI_FLUSH | MI_NO_WRITE_FLUSH;
  705. if ((invalidate_domains|flush_domains) &
  706. I915_GEM_DOMAIN_RENDER)
  707. cmd &= ~MI_NO_WRITE_FLUSH;
  708. if (!IS_I965G(dev)) {
  709. /*
  710. * On the 965, the sampler cache always gets flushed
  711. * and this bit is reserved.
  712. */
  713. if (invalidate_domains & I915_GEM_DOMAIN_SAMPLER)
  714. cmd |= MI_READ_FLUSH;
  715. }
  716. if (invalidate_domains & I915_GEM_DOMAIN_INSTRUCTION)
  717. cmd |= MI_EXE_FLUSH;
  718. #if WATCH_EXEC
  719. DRM_INFO("%s: queue flush %08x to ring\n", __func__, cmd);
  720. #endif
  721. BEGIN_LP_RING(2);
  722. OUT_RING(cmd);
  723. OUT_RING(0); /* noop */
  724. ADVANCE_LP_RING();
  725. }
  726. }
  727. /**
  728. * Ensures that all rendering to the object has completed and the object is
  729. * safe to unbind from the GTT or access from the CPU.
  730. */
  731. static int
  732. i915_gem_object_wait_rendering(struct drm_gem_object *obj)
  733. {
  734. struct drm_device *dev = obj->dev;
  735. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  736. int ret;
  737. /* If there are writes queued to the buffer, flush and
  738. * create a new seqno to wait for.
  739. */
  740. if (obj->write_domain & ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT)) {
  741. uint32_t write_domain = obj->write_domain;
  742. #if WATCH_BUF
  743. DRM_INFO("%s: flushing object %p from write domain %08x\n",
  744. __func__, obj, write_domain);
  745. #endif
  746. i915_gem_flush(dev, 0, write_domain);
  747. i915_gem_object_move_to_active(obj);
  748. obj_priv->last_rendering_seqno = i915_add_request(dev,
  749. write_domain);
  750. BUG_ON(obj_priv->last_rendering_seqno == 0);
  751. #if WATCH_LRU
  752. DRM_INFO("%s: flush moves to exec list %p\n", __func__, obj);
  753. #endif
  754. }
  755. /* If there is rendering queued on the buffer being evicted, wait for
  756. * it.
  757. */
  758. if (obj_priv->active) {
  759. #if WATCH_BUF
  760. DRM_INFO("%s: object %p wait for seqno %08x\n",
  761. __func__, obj, obj_priv->last_rendering_seqno);
  762. #endif
  763. ret = i915_wait_request(dev, obj_priv->last_rendering_seqno);
  764. if (ret != 0)
  765. return ret;
  766. }
  767. return 0;
  768. }
  769. /**
  770. * Unbinds an object from the GTT aperture.
  771. */
  772. static int
  773. i915_gem_object_unbind(struct drm_gem_object *obj)
  774. {
  775. struct drm_device *dev = obj->dev;
  776. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  777. int ret = 0;
  778. #if WATCH_BUF
  779. DRM_INFO("%s:%d %p\n", __func__, __LINE__, obj);
  780. DRM_INFO("gtt_space %p\n", obj_priv->gtt_space);
  781. #endif
  782. if (obj_priv->gtt_space == NULL)
  783. return 0;
  784. if (obj_priv->pin_count != 0) {
  785. DRM_ERROR("Attempting to unbind pinned buffer\n");
  786. return -EINVAL;
  787. }
  788. /* Wait for any rendering to complete
  789. */
  790. ret = i915_gem_object_wait_rendering(obj);
  791. if (ret) {
  792. DRM_ERROR("wait_rendering failed: %d\n", ret);
  793. return ret;
  794. }
  795. /* Move the object to the CPU domain to ensure that
  796. * any possible CPU writes while it's not in the GTT
  797. * are flushed when we go to remap it. This will
  798. * also ensure that all pending GPU writes are finished
  799. * before we unbind.
  800. */
  801. ret = i915_gem_object_set_domain(obj, I915_GEM_DOMAIN_CPU,
  802. I915_GEM_DOMAIN_CPU);
  803. if (ret) {
  804. DRM_ERROR("set_domain failed: %d\n", ret);
  805. return ret;
  806. }
  807. if (obj_priv->agp_mem != NULL) {
  808. drm_unbind_agp(obj_priv->agp_mem);
  809. drm_free_agp(obj_priv->agp_mem, obj->size / PAGE_SIZE);
  810. obj_priv->agp_mem = NULL;
  811. }
  812. BUG_ON(obj_priv->active);
  813. i915_gem_object_free_page_list(obj);
  814. if (obj_priv->gtt_space) {
  815. atomic_dec(&dev->gtt_count);
  816. atomic_sub(obj->size, &dev->gtt_memory);
  817. drm_mm_put_block(obj_priv->gtt_space);
  818. obj_priv->gtt_space = NULL;
  819. }
  820. /* Remove ourselves from the LRU list if present. */
  821. if (!list_empty(&obj_priv->list))
  822. list_del_init(&obj_priv->list);
  823. return 0;
  824. }
  825. static int
  826. i915_gem_evict_something(struct drm_device *dev)
  827. {
  828. drm_i915_private_t *dev_priv = dev->dev_private;
  829. struct drm_gem_object *obj;
  830. struct drm_i915_gem_object *obj_priv;
  831. int ret = 0;
  832. for (;;) {
  833. /* If there's an inactive buffer available now, grab it
  834. * and be done.
  835. */
  836. if (!list_empty(&dev_priv->mm.inactive_list)) {
  837. obj_priv = list_first_entry(&dev_priv->mm.inactive_list,
  838. struct drm_i915_gem_object,
  839. list);
  840. obj = obj_priv->obj;
  841. BUG_ON(obj_priv->pin_count != 0);
  842. #if WATCH_LRU
  843. DRM_INFO("%s: evicting %p\n", __func__, obj);
  844. #endif
  845. BUG_ON(obj_priv->active);
  846. /* Wait on the rendering and unbind the buffer. */
  847. ret = i915_gem_object_unbind(obj);
  848. break;
  849. }
  850. /* If we didn't get anything, but the ring is still processing
  851. * things, wait for one of those things to finish and hopefully
  852. * leave us a buffer to evict.
  853. */
  854. if (!list_empty(&dev_priv->mm.request_list)) {
  855. struct drm_i915_gem_request *request;
  856. request = list_first_entry(&dev_priv->mm.request_list,
  857. struct drm_i915_gem_request,
  858. list);
  859. ret = i915_wait_request(dev, request->seqno);
  860. if (ret)
  861. break;
  862. /* if waiting caused an object to become inactive,
  863. * then loop around and wait for it. Otherwise, we
  864. * assume that waiting freed and unbound something,
  865. * so there should now be some space in the GTT
  866. */
  867. if (!list_empty(&dev_priv->mm.inactive_list))
  868. continue;
  869. break;
  870. }
  871. /* If we didn't have anything on the request list but there
  872. * are buffers awaiting a flush, emit one and try again.
  873. * When we wait on it, those buffers waiting for that flush
  874. * will get moved to inactive.
  875. */
  876. if (!list_empty(&dev_priv->mm.flushing_list)) {
  877. obj_priv = list_first_entry(&dev_priv->mm.flushing_list,
  878. struct drm_i915_gem_object,
  879. list);
  880. obj = obj_priv->obj;
  881. i915_gem_flush(dev,
  882. obj->write_domain,
  883. obj->write_domain);
  884. i915_add_request(dev, obj->write_domain);
  885. obj = NULL;
  886. continue;
  887. }
  888. DRM_ERROR("inactive empty %d request empty %d "
  889. "flushing empty %d\n",
  890. list_empty(&dev_priv->mm.inactive_list),
  891. list_empty(&dev_priv->mm.request_list),
  892. list_empty(&dev_priv->mm.flushing_list));
  893. /* If we didn't do any of the above, there's nothing to be done
  894. * and we just can't fit it in.
  895. */
  896. return -ENOMEM;
  897. }
  898. return ret;
  899. }
  900. static int
  901. i915_gem_object_get_page_list(struct drm_gem_object *obj)
  902. {
  903. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  904. int page_count, i;
  905. struct address_space *mapping;
  906. struct inode *inode;
  907. struct page *page;
  908. int ret;
  909. if (obj_priv->page_list)
  910. return 0;
  911. /* Get the list of pages out of our struct file. They'll be pinned
  912. * at this point until we release them.
  913. */
  914. page_count = obj->size / PAGE_SIZE;
  915. BUG_ON(obj_priv->page_list != NULL);
  916. obj_priv->page_list = drm_calloc(page_count, sizeof(struct page *),
  917. DRM_MEM_DRIVER);
  918. if (obj_priv->page_list == NULL) {
  919. DRM_ERROR("Faled to allocate page list\n");
  920. return -ENOMEM;
  921. }
  922. inode = obj->filp->f_path.dentry->d_inode;
  923. mapping = inode->i_mapping;
  924. for (i = 0; i < page_count; i++) {
  925. page = read_mapping_page(mapping, i, NULL);
  926. if (IS_ERR(page)) {
  927. ret = PTR_ERR(page);
  928. DRM_ERROR("read_mapping_page failed: %d\n", ret);
  929. i915_gem_object_free_page_list(obj);
  930. return ret;
  931. }
  932. obj_priv->page_list[i] = page;
  933. }
  934. return 0;
  935. }
  936. /**
  937. * Finds free space in the GTT aperture and binds the object there.
  938. */
  939. static int
  940. i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment)
  941. {
  942. struct drm_device *dev = obj->dev;
  943. drm_i915_private_t *dev_priv = dev->dev_private;
  944. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  945. struct drm_mm_node *free_space;
  946. int page_count, ret;
  947. if (alignment == 0)
  948. alignment = PAGE_SIZE;
  949. if (alignment & (PAGE_SIZE - 1)) {
  950. DRM_ERROR("Invalid object alignment requested %u\n", alignment);
  951. return -EINVAL;
  952. }
  953. search_free:
  954. free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
  955. obj->size, alignment, 0);
  956. if (free_space != NULL) {
  957. obj_priv->gtt_space = drm_mm_get_block(free_space, obj->size,
  958. alignment);
  959. if (obj_priv->gtt_space != NULL) {
  960. obj_priv->gtt_space->private = obj;
  961. obj_priv->gtt_offset = obj_priv->gtt_space->start;
  962. }
  963. }
  964. if (obj_priv->gtt_space == NULL) {
  965. /* If the gtt is empty and we're still having trouble
  966. * fitting our object in, we're out of memory.
  967. */
  968. #if WATCH_LRU
  969. DRM_INFO("%s: GTT full, evicting something\n", __func__);
  970. #endif
  971. if (list_empty(&dev_priv->mm.inactive_list) &&
  972. list_empty(&dev_priv->mm.flushing_list) &&
  973. list_empty(&dev_priv->mm.active_list)) {
  974. DRM_ERROR("GTT full, but LRU list empty\n");
  975. return -ENOMEM;
  976. }
  977. ret = i915_gem_evict_something(dev);
  978. if (ret != 0) {
  979. DRM_ERROR("Failed to evict a buffer %d\n", ret);
  980. return ret;
  981. }
  982. goto search_free;
  983. }
  984. #if WATCH_BUF
  985. DRM_INFO("Binding object of size %d at 0x%08x\n",
  986. obj->size, obj_priv->gtt_offset);
  987. #endif
  988. ret = i915_gem_object_get_page_list(obj);
  989. if (ret) {
  990. drm_mm_put_block(obj_priv->gtt_space);
  991. obj_priv->gtt_space = NULL;
  992. return ret;
  993. }
  994. page_count = obj->size / PAGE_SIZE;
  995. /* Create an AGP memory structure pointing at our pages, and bind it
  996. * into the GTT.
  997. */
  998. obj_priv->agp_mem = drm_agp_bind_pages(dev,
  999. obj_priv->page_list,
  1000. page_count,
  1001. obj_priv->gtt_offset);
  1002. if (obj_priv->agp_mem == NULL) {
  1003. i915_gem_object_free_page_list(obj);
  1004. drm_mm_put_block(obj_priv->gtt_space);
  1005. obj_priv->gtt_space = NULL;
  1006. return -ENOMEM;
  1007. }
  1008. atomic_inc(&dev->gtt_count);
  1009. atomic_add(obj->size, &dev->gtt_memory);
  1010. /* Assert that the object is not currently in any GPU domain. As it
  1011. * wasn't in the GTT, there shouldn't be any way it could have been in
  1012. * a GPU cache
  1013. */
  1014. BUG_ON(obj->read_domains & ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT));
  1015. BUG_ON(obj->write_domain & ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT));
  1016. return 0;
  1017. }
  1018. void
  1019. i915_gem_clflush_object(struct drm_gem_object *obj)
  1020. {
  1021. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  1022. /* If we don't have a page list set up, then we're not pinned
  1023. * to GPU, and we can ignore the cache flush because it'll happen
  1024. * again at bind time.
  1025. */
  1026. if (obj_priv->page_list == NULL)
  1027. return;
  1028. drm_clflush_pages(obj_priv->page_list, obj->size / PAGE_SIZE);
  1029. }
  1030. /*
  1031. * Set the next domain for the specified object. This
  1032. * may not actually perform the necessary flushing/invaliding though,
  1033. * as that may want to be batched with other set_domain operations
  1034. *
  1035. * This is (we hope) the only really tricky part of gem. The goal
  1036. * is fairly simple -- track which caches hold bits of the object
  1037. * and make sure they remain coherent. A few concrete examples may
  1038. * help to explain how it works. For shorthand, we use the notation
  1039. * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the
  1040. * a pair of read and write domain masks.
  1041. *
  1042. * Case 1: the batch buffer
  1043. *
  1044. * 1. Allocated
  1045. * 2. Written by CPU
  1046. * 3. Mapped to GTT
  1047. * 4. Read by GPU
  1048. * 5. Unmapped from GTT
  1049. * 6. Freed
  1050. *
  1051. * Let's take these a step at a time
  1052. *
  1053. * 1. Allocated
  1054. * Pages allocated from the kernel may still have
  1055. * cache contents, so we set them to (CPU, CPU) always.
  1056. * 2. Written by CPU (using pwrite)
  1057. * The pwrite function calls set_domain (CPU, CPU) and
  1058. * this function does nothing (as nothing changes)
  1059. * 3. Mapped by GTT
  1060. * This function asserts that the object is not
  1061. * currently in any GPU-based read or write domains
  1062. * 4. Read by GPU
  1063. * i915_gem_execbuffer calls set_domain (COMMAND, 0).
  1064. * As write_domain is zero, this function adds in the
  1065. * current read domains (CPU+COMMAND, 0).
  1066. * flush_domains is set to CPU.
  1067. * invalidate_domains is set to COMMAND
  1068. * clflush is run to get data out of the CPU caches
  1069. * then i915_dev_set_domain calls i915_gem_flush to
  1070. * emit an MI_FLUSH and drm_agp_chipset_flush
  1071. * 5. Unmapped from GTT
  1072. * i915_gem_object_unbind calls set_domain (CPU, CPU)
  1073. * flush_domains and invalidate_domains end up both zero
  1074. * so no flushing/invalidating happens
  1075. * 6. Freed
  1076. * yay, done
  1077. *
  1078. * Case 2: The shared render buffer
  1079. *
  1080. * 1. Allocated
  1081. * 2. Mapped to GTT
  1082. * 3. Read/written by GPU
  1083. * 4. set_domain to (CPU,CPU)
  1084. * 5. Read/written by CPU
  1085. * 6. Read/written by GPU
  1086. *
  1087. * 1. Allocated
  1088. * Same as last example, (CPU, CPU)
  1089. * 2. Mapped to GTT
  1090. * Nothing changes (assertions find that it is not in the GPU)
  1091. * 3. Read/written by GPU
  1092. * execbuffer calls set_domain (RENDER, RENDER)
  1093. * flush_domains gets CPU
  1094. * invalidate_domains gets GPU
  1095. * clflush (obj)
  1096. * MI_FLUSH and drm_agp_chipset_flush
  1097. * 4. set_domain (CPU, CPU)
  1098. * flush_domains gets GPU
  1099. * invalidate_domains gets CPU
  1100. * wait_rendering (obj) to make sure all drawing is complete.
  1101. * This will include an MI_FLUSH to get the data from GPU
  1102. * to memory
  1103. * clflush (obj) to invalidate the CPU cache
  1104. * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?)
  1105. * 5. Read/written by CPU
  1106. * cache lines are loaded and dirtied
  1107. * 6. Read written by GPU
  1108. * Same as last GPU access
  1109. *
  1110. * Case 3: The constant buffer
  1111. *
  1112. * 1. Allocated
  1113. * 2. Written by CPU
  1114. * 3. Read by GPU
  1115. * 4. Updated (written) by CPU again
  1116. * 5. Read by GPU
  1117. *
  1118. * 1. Allocated
  1119. * (CPU, CPU)
  1120. * 2. Written by CPU
  1121. * (CPU, CPU)
  1122. * 3. Read by GPU
  1123. * (CPU+RENDER, 0)
  1124. * flush_domains = CPU
  1125. * invalidate_domains = RENDER
  1126. * clflush (obj)
  1127. * MI_FLUSH
  1128. * drm_agp_chipset_flush
  1129. * 4. Updated (written) by CPU again
  1130. * (CPU, CPU)
  1131. * flush_domains = 0 (no previous write domain)
  1132. * invalidate_domains = 0 (no new read domains)
  1133. * 5. Read by GPU
  1134. * (CPU+RENDER, 0)
  1135. * flush_domains = CPU
  1136. * invalidate_domains = RENDER
  1137. * clflush (obj)
  1138. * MI_FLUSH
  1139. * drm_agp_chipset_flush
  1140. */
  1141. static int
  1142. i915_gem_object_set_domain(struct drm_gem_object *obj,
  1143. uint32_t read_domains,
  1144. uint32_t write_domain)
  1145. {
  1146. struct drm_device *dev = obj->dev;
  1147. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  1148. uint32_t invalidate_domains = 0;
  1149. uint32_t flush_domains = 0;
  1150. int ret;
  1151. #if WATCH_BUF
  1152. DRM_INFO("%s: object %p read %08x -> %08x write %08x -> %08x\n",
  1153. __func__, obj,
  1154. obj->read_domains, read_domains,
  1155. obj->write_domain, write_domain);
  1156. #endif
  1157. /*
  1158. * If the object isn't moving to a new write domain,
  1159. * let the object stay in multiple read domains
  1160. */
  1161. if (write_domain == 0)
  1162. read_domains |= obj->read_domains;
  1163. else
  1164. obj_priv->dirty = 1;
  1165. /*
  1166. * Flush the current write domain if
  1167. * the new read domains don't match. Invalidate
  1168. * any read domains which differ from the old
  1169. * write domain
  1170. */
  1171. if (obj->write_domain && obj->write_domain != read_domains) {
  1172. flush_domains |= obj->write_domain;
  1173. invalidate_domains |= read_domains & ~obj->write_domain;
  1174. }
  1175. /*
  1176. * Invalidate any read caches which may have
  1177. * stale data. That is, any new read domains.
  1178. */
  1179. invalidate_domains |= read_domains & ~obj->read_domains;
  1180. if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU) {
  1181. #if WATCH_BUF
  1182. DRM_INFO("%s: CPU domain flush %08x invalidate %08x\n",
  1183. __func__, flush_domains, invalidate_domains);
  1184. #endif
  1185. /*
  1186. * If we're invaliding the CPU cache and flushing a GPU cache,
  1187. * then pause for rendering so that the GPU caches will be
  1188. * flushed before the cpu cache is invalidated
  1189. */
  1190. if ((invalidate_domains & I915_GEM_DOMAIN_CPU) &&
  1191. (flush_domains & ~(I915_GEM_DOMAIN_CPU |
  1192. I915_GEM_DOMAIN_GTT))) {
  1193. ret = i915_gem_object_wait_rendering(obj);
  1194. if (ret)
  1195. return ret;
  1196. }
  1197. i915_gem_clflush_object(obj);
  1198. }
  1199. if ((write_domain | flush_domains) != 0)
  1200. obj->write_domain = write_domain;
  1201. /* If we're invalidating the CPU domain, clear the per-page CPU
  1202. * domain list as well.
  1203. */
  1204. if (obj_priv->page_cpu_valid != NULL &&
  1205. (write_domain != 0 ||
  1206. read_domains & I915_GEM_DOMAIN_CPU)) {
  1207. drm_free(obj_priv->page_cpu_valid, obj->size / PAGE_SIZE,
  1208. DRM_MEM_DRIVER);
  1209. obj_priv->page_cpu_valid = NULL;
  1210. }
  1211. obj->read_domains = read_domains;
  1212. dev->invalidate_domains |= invalidate_domains;
  1213. dev->flush_domains |= flush_domains;
  1214. #if WATCH_BUF
  1215. DRM_INFO("%s: read %08x write %08x invalidate %08x flush %08x\n",
  1216. __func__,
  1217. obj->read_domains, obj->write_domain,
  1218. dev->invalidate_domains, dev->flush_domains);
  1219. #endif
  1220. return 0;
  1221. }
  1222. /**
  1223. * Set the read/write domain on a range of the object.
  1224. *
  1225. * Currently only implemented for CPU reads, otherwise drops to normal
  1226. * i915_gem_object_set_domain().
  1227. */
  1228. static int
  1229. i915_gem_object_set_domain_range(struct drm_gem_object *obj,
  1230. uint64_t offset,
  1231. uint64_t size,
  1232. uint32_t read_domains,
  1233. uint32_t write_domain)
  1234. {
  1235. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  1236. int ret, i;
  1237. if (obj->read_domains & I915_GEM_DOMAIN_CPU)
  1238. return 0;
  1239. if (read_domains != I915_GEM_DOMAIN_CPU ||
  1240. write_domain != 0)
  1241. return i915_gem_object_set_domain(obj,
  1242. read_domains, write_domain);
  1243. /* Wait on any GPU rendering to the object to be flushed. */
  1244. if (obj->write_domain & ~(I915_GEM_DOMAIN_CPU | I915_GEM_DOMAIN_GTT)) {
  1245. ret = i915_gem_object_wait_rendering(obj);
  1246. if (ret)
  1247. return ret;
  1248. }
  1249. if (obj_priv->page_cpu_valid == NULL) {
  1250. obj_priv->page_cpu_valid = drm_calloc(1, obj->size / PAGE_SIZE,
  1251. DRM_MEM_DRIVER);
  1252. }
  1253. /* Flush the cache on any pages that are still invalid from the CPU's
  1254. * perspective.
  1255. */
  1256. for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE; i++) {
  1257. if (obj_priv->page_cpu_valid[i])
  1258. continue;
  1259. drm_clflush_pages(obj_priv->page_list + i, 1);
  1260. obj_priv->page_cpu_valid[i] = 1;
  1261. }
  1262. return 0;
  1263. }
  1264. /**
  1265. * Once all of the objects have been set in the proper domain,
  1266. * perform the necessary flush and invalidate operations.
  1267. *
  1268. * Returns the write domains flushed, for use in flush tracking.
  1269. */
  1270. static uint32_t
  1271. i915_gem_dev_set_domain(struct drm_device *dev)
  1272. {
  1273. uint32_t flush_domains = dev->flush_domains;
  1274. /*
  1275. * Now that all the buffers are synced to the proper domains,
  1276. * flush and invalidate the collected domains
  1277. */
  1278. if (dev->invalidate_domains | dev->flush_domains) {
  1279. #if WATCH_EXEC
  1280. DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n",
  1281. __func__,
  1282. dev->invalidate_domains,
  1283. dev->flush_domains);
  1284. #endif
  1285. i915_gem_flush(dev,
  1286. dev->invalidate_domains,
  1287. dev->flush_domains);
  1288. dev->invalidate_domains = 0;
  1289. dev->flush_domains = 0;
  1290. }
  1291. return flush_domains;
  1292. }
  1293. /**
  1294. * Pin an object to the GTT and evaluate the relocations landing in it.
  1295. */
  1296. static int
  1297. i915_gem_object_pin_and_relocate(struct drm_gem_object *obj,
  1298. struct drm_file *file_priv,
  1299. struct drm_i915_gem_exec_object *entry)
  1300. {
  1301. struct drm_device *dev = obj->dev;
  1302. struct drm_i915_gem_relocation_entry reloc;
  1303. struct drm_i915_gem_relocation_entry __user *relocs;
  1304. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  1305. int i, ret;
  1306. uint32_t last_reloc_offset = -1;
  1307. void __iomem *reloc_page = NULL;
  1308. /* Choose the GTT offset for our buffer and put it there. */
  1309. ret = i915_gem_object_pin(obj, (uint32_t) entry->alignment);
  1310. if (ret)
  1311. return ret;
  1312. entry->offset = obj_priv->gtt_offset;
  1313. relocs = (struct drm_i915_gem_relocation_entry __user *)
  1314. (uintptr_t) entry->relocs_ptr;
  1315. /* Apply the relocations, using the GTT aperture to avoid cache
  1316. * flushing requirements.
  1317. */
  1318. for (i = 0; i < entry->relocation_count; i++) {
  1319. struct drm_gem_object *target_obj;
  1320. struct drm_i915_gem_object *target_obj_priv;
  1321. uint32_t reloc_val, reloc_offset;
  1322. uint32_t __iomem *reloc_entry;
  1323. ret = copy_from_user(&reloc, relocs + i, sizeof(reloc));
  1324. if (ret != 0) {
  1325. i915_gem_object_unpin(obj);
  1326. return ret;
  1327. }
  1328. target_obj = drm_gem_object_lookup(obj->dev, file_priv,
  1329. reloc.target_handle);
  1330. if (target_obj == NULL) {
  1331. i915_gem_object_unpin(obj);
  1332. return -EBADF;
  1333. }
  1334. target_obj_priv = target_obj->driver_private;
  1335. /* The target buffer should have appeared before us in the
  1336. * exec_object list, so it should have a GTT space bound by now.
  1337. */
  1338. if (target_obj_priv->gtt_space == NULL) {
  1339. DRM_ERROR("No GTT space found for object %d\n",
  1340. reloc.target_handle);
  1341. drm_gem_object_unreference(target_obj);
  1342. i915_gem_object_unpin(obj);
  1343. return -EINVAL;
  1344. }
  1345. if (reloc.offset > obj->size - 4) {
  1346. DRM_ERROR("Relocation beyond object bounds: "
  1347. "obj %p target %d offset %d size %d.\n",
  1348. obj, reloc.target_handle,
  1349. (int) reloc.offset, (int) obj->size);
  1350. drm_gem_object_unreference(target_obj);
  1351. i915_gem_object_unpin(obj);
  1352. return -EINVAL;
  1353. }
  1354. if (reloc.offset & 3) {
  1355. DRM_ERROR("Relocation not 4-byte aligned: "
  1356. "obj %p target %d offset %d.\n",
  1357. obj, reloc.target_handle,
  1358. (int) reloc.offset);
  1359. drm_gem_object_unreference(target_obj);
  1360. i915_gem_object_unpin(obj);
  1361. return -EINVAL;
  1362. }
  1363. if (reloc.write_domain && target_obj->pending_write_domain &&
  1364. reloc.write_domain != target_obj->pending_write_domain) {
  1365. DRM_ERROR("Write domain conflict: "
  1366. "obj %p target %d offset %d "
  1367. "new %08x old %08x\n",
  1368. obj, reloc.target_handle,
  1369. (int) reloc.offset,
  1370. reloc.write_domain,
  1371. target_obj->pending_write_domain);
  1372. drm_gem_object_unreference(target_obj);
  1373. i915_gem_object_unpin(obj);
  1374. return -EINVAL;
  1375. }
  1376. #if WATCH_RELOC
  1377. DRM_INFO("%s: obj %p offset %08x target %d "
  1378. "read %08x write %08x gtt %08x "
  1379. "presumed %08x delta %08x\n",
  1380. __func__,
  1381. obj,
  1382. (int) reloc.offset,
  1383. (int) reloc.target_handle,
  1384. (int) reloc.read_domains,
  1385. (int) reloc.write_domain,
  1386. (int) target_obj_priv->gtt_offset,
  1387. (int) reloc.presumed_offset,
  1388. reloc.delta);
  1389. #endif
  1390. target_obj->pending_read_domains |= reloc.read_domains;
  1391. target_obj->pending_write_domain |= reloc.write_domain;
  1392. /* If the relocation already has the right value in it, no
  1393. * more work needs to be done.
  1394. */
  1395. if (target_obj_priv->gtt_offset == reloc.presumed_offset) {
  1396. drm_gem_object_unreference(target_obj);
  1397. continue;
  1398. }
  1399. /* Now that we're going to actually write some data in,
  1400. * make sure that any rendering using this buffer's contents
  1401. * is completed.
  1402. */
  1403. i915_gem_object_wait_rendering(obj);
  1404. /* As we're writing through the gtt, flush
  1405. * any CPU writes before we write the relocations
  1406. */
  1407. if (obj->write_domain & I915_GEM_DOMAIN_CPU) {
  1408. i915_gem_clflush_object(obj);
  1409. drm_agp_chipset_flush(dev);
  1410. obj->write_domain = 0;
  1411. }
  1412. /* Map the page containing the relocation we're going to
  1413. * perform.
  1414. */
  1415. reloc_offset = obj_priv->gtt_offset + reloc.offset;
  1416. if (reloc_page == NULL ||
  1417. (last_reloc_offset & ~(PAGE_SIZE - 1)) !=
  1418. (reloc_offset & ~(PAGE_SIZE - 1))) {
  1419. if (reloc_page != NULL)
  1420. iounmap(reloc_page);
  1421. reloc_page = ioremap_wc(dev->agp->base +
  1422. (reloc_offset &
  1423. ~(PAGE_SIZE - 1)),
  1424. PAGE_SIZE);
  1425. last_reloc_offset = reloc_offset;
  1426. if (reloc_page == NULL) {
  1427. drm_gem_object_unreference(target_obj);
  1428. i915_gem_object_unpin(obj);
  1429. return -ENOMEM;
  1430. }
  1431. }
  1432. reloc_entry = (uint32_t __iomem *)(reloc_page +
  1433. (reloc_offset & (PAGE_SIZE - 1)));
  1434. reloc_val = target_obj_priv->gtt_offset + reloc.delta;
  1435. #if WATCH_BUF
  1436. DRM_INFO("Applied relocation: %p@0x%08x %08x -> %08x\n",
  1437. obj, (unsigned int) reloc.offset,
  1438. readl(reloc_entry), reloc_val);
  1439. #endif
  1440. writel(reloc_val, reloc_entry);
  1441. /* Write the updated presumed offset for this entry back out
  1442. * to the user.
  1443. */
  1444. reloc.presumed_offset = target_obj_priv->gtt_offset;
  1445. ret = copy_to_user(relocs + i, &reloc, sizeof(reloc));
  1446. if (ret != 0) {
  1447. drm_gem_object_unreference(target_obj);
  1448. i915_gem_object_unpin(obj);
  1449. return ret;
  1450. }
  1451. drm_gem_object_unreference(target_obj);
  1452. }
  1453. if (reloc_page != NULL)
  1454. iounmap(reloc_page);
  1455. #if WATCH_BUF
  1456. if (0)
  1457. i915_gem_dump_object(obj, 128, __func__, ~0);
  1458. #endif
  1459. return 0;
  1460. }
  1461. /** Dispatch a batchbuffer to the ring
  1462. */
  1463. static int
  1464. i915_dispatch_gem_execbuffer(struct drm_device *dev,
  1465. struct drm_i915_gem_execbuffer *exec,
  1466. uint64_t exec_offset)
  1467. {
  1468. drm_i915_private_t *dev_priv = dev->dev_private;
  1469. struct drm_clip_rect __user *boxes = (struct drm_clip_rect __user *)
  1470. (uintptr_t) exec->cliprects_ptr;
  1471. int nbox = exec->num_cliprects;
  1472. int i = 0, count;
  1473. uint32_t exec_start, exec_len;
  1474. RING_LOCALS;
  1475. exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
  1476. exec_len = (uint32_t) exec->batch_len;
  1477. if ((exec_start | exec_len) & 0x7) {
  1478. DRM_ERROR("alignment\n");
  1479. return -EINVAL;
  1480. }
  1481. if (!exec_start)
  1482. return -EINVAL;
  1483. count = nbox ? nbox : 1;
  1484. for (i = 0; i < count; i++) {
  1485. if (i < nbox) {
  1486. int ret = i915_emit_box(dev, boxes, i,
  1487. exec->DR1, exec->DR4);
  1488. if (ret)
  1489. return ret;
  1490. }
  1491. if (IS_I830(dev) || IS_845G(dev)) {
  1492. BEGIN_LP_RING(4);
  1493. OUT_RING(MI_BATCH_BUFFER);
  1494. OUT_RING(exec_start | MI_BATCH_NON_SECURE);
  1495. OUT_RING(exec_start + exec_len - 4);
  1496. OUT_RING(0);
  1497. ADVANCE_LP_RING();
  1498. } else {
  1499. BEGIN_LP_RING(2);
  1500. if (IS_I965G(dev)) {
  1501. OUT_RING(MI_BATCH_BUFFER_START |
  1502. (2 << 6) |
  1503. MI_BATCH_NON_SECURE_I965);
  1504. OUT_RING(exec_start);
  1505. } else {
  1506. OUT_RING(MI_BATCH_BUFFER_START |
  1507. (2 << 6));
  1508. OUT_RING(exec_start | MI_BATCH_NON_SECURE);
  1509. }
  1510. ADVANCE_LP_RING();
  1511. }
  1512. }
  1513. /* XXX breadcrumb */
  1514. return 0;
  1515. }
  1516. /* Throttle our rendering by waiting until the ring has completed our requests
  1517. * emitted over 20 msec ago.
  1518. *
  1519. * This should get us reasonable parallelism between CPU and GPU but also
  1520. * relatively low latency when blocking on a particular request to finish.
  1521. */
  1522. static int
  1523. i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file_priv)
  1524. {
  1525. struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
  1526. int ret = 0;
  1527. uint32_t seqno;
  1528. mutex_lock(&dev->struct_mutex);
  1529. seqno = i915_file_priv->mm.last_gem_throttle_seqno;
  1530. i915_file_priv->mm.last_gem_throttle_seqno =
  1531. i915_file_priv->mm.last_gem_seqno;
  1532. if (seqno)
  1533. ret = i915_wait_request(dev, seqno);
  1534. mutex_unlock(&dev->struct_mutex);
  1535. return ret;
  1536. }
  1537. int
  1538. i915_gem_execbuffer(struct drm_device *dev, void *data,
  1539. struct drm_file *file_priv)
  1540. {
  1541. drm_i915_private_t *dev_priv = dev->dev_private;
  1542. struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
  1543. struct drm_i915_gem_execbuffer *args = data;
  1544. struct drm_i915_gem_exec_object *exec_list = NULL;
  1545. struct drm_gem_object **object_list = NULL;
  1546. struct drm_gem_object *batch_obj;
  1547. int ret, i, pinned = 0;
  1548. uint64_t exec_offset;
  1549. uint32_t seqno, flush_domains;
  1550. #if WATCH_EXEC
  1551. DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
  1552. (int) args->buffers_ptr, args->buffer_count, args->batch_len);
  1553. #endif
  1554. if (args->buffer_count < 1) {
  1555. DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
  1556. return -EINVAL;
  1557. }
  1558. /* Copy in the exec list from userland */
  1559. exec_list = drm_calloc(sizeof(*exec_list), args->buffer_count,
  1560. DRM_MEM_DRIVER);
  1561. object_list = drm_calloc(sizeof(*object_list), args->buffer_count,
  1562. DRM_MEM_DRIVER);
  1563. if (exec_list == NULL || object_list == NULL) {
  1564. DRM_ERROR("Failed to allocate exec or object list "
  1565. "for %d buffers\n",
  1566. args->buffer_count);
  1567. ret = -ENOMEM;
  1568. goto pre_mutex_err;
  1569. }
  1570. ret = copy_from_user(exec_list,
  1571. (struct drm_i915_relocation_entry __user *)
  1572. (uintptr_t) args->buffers_ptr,
  1573. sizeof(*exec_list) * args->buffer_count);
  1574. if (ret != 0) {
  1575. DRM_ERROR("copy %d exec entries failed %d\n",
  1576. args->buffer_count, ret);
  1577. goto pre_mutex_err;
  1578. }
  1579. mutex_lock(&dev->struct_mutex);
  1580. i915_verify_inactive(dev, __FILE__, __LINE__);
  1581. if (dev_priv->mm.wedged) {
  1582. DRM_ERROR("Execbuf while wedged\n");
  1583. mutex_unlock(&dev->struct_mutex);
  1584. return -EIO;
  1585. }
  1586. if (dev_priv->mm.suspended) {
  1587. DRM_ERROR("Execbuf while VT-switched.\n");
  1588. mutex_unlock(&dev->struct_mutex);
  1589. return -EBUSY;
  1590. }
  1591. /* Zero the gloabl flush/invalidate flags. These
  1592. * will be modified as each object is bound to the
  1593. * gtt
  1594. */
  1595. dev->invalidate_domains = 0;
  1596. dev->flush_domains = 0;
  1597. /* Look up object handles and perform the relocations */
  1598. for (i = 0; i < args->buffer_count; i++) {
  1599. object_list[i] = drm_gem_object_lookup(dev, file_priv,
  1600. exec_list[i].handle);
  1601. if (object_list[i] == NULL) {
  1602. DRM_ERROR("Invalid object handle %d at index %d\n",
  1603. exec_list[i].handle, i);
  1604. ret = -EBADF;
  1605. goto err;
  1606. }
  1607. object_list[i]->pending_read_domains = 0;
  1608. object_list[i]->pending_write_domain = 0;
  1609. ret = i915_gem_object_pin_and_relocate(object_list[i],
  1610. file_priv,
  1611. &exec_list[i]);
  1612. if (ret) {
  1613. DRM_ERROR("object bind and relocate failed %d\n", ret);
  1614. goto err;
  1615. }
  1616. pinned = i + 1;
  1617. }
  1618. /* Set the pending read domains for the batch buffer to COMMAND */
  1619. batch_obj = object_list[args->buffer_count-1];
  1620. batch_obj->pending_read_domains = I915_GEM_DOMAIN_COMMAND;
  1621. batch_obj->pending_write_domain = 0;
  1622. i915_verify_inactive(dev, __FILE__, __LINE__);
  1623. for (i = 0; i < args->buffer_count; i++) {
  1624. struct drm_gem_object *obj = object_list[i];
  1625. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  1626. if (obj_priv->gtt_space == NULL) {
  1627. /* We evicted the buffer in the process of validating
  1628. * our set of buffers in. We could try to recover by
  1629. * kicking them everything out and trying again from
  1630. * the start.
  1631. */
  1632. ret = -ENOMEM;
  1633. goto err;
  1634. }
  1635. /* make sure all previous memory operations have passed */
  1636. ret = i915_gem_object_set_domain(obj,
  1637. obj->pending_read_domains,
  1638. obj->pending_write_domain);
  1639. if (ret)
  1640. goto err;
  1641. }
  1642. i915_verify_inactive(dev, __FILE__, __LINE__);
  1643. /* Flush/invalidate caches and chipset buffer */
  1644. flush_domains = i915_gem_dev_set_domain(dev);
  1645. i915_verify_inactive(dev, __FILE__, __LINE__);
  1646. #if WATCH_COHERENCY
  1647. for (i = 0; i < args->buffer_count; i++) {
  1648. i915_gem_object_check_coherency(object_list[i],
  1649. exec_list[i].handle);
  1650. }
  1651. #endif
  1652. exec_offset = exec_list[args->buffer_count - 1].offset;
  1653. #if WATCH_EXEC
  1654. i915_gem_dump_object(object_list[args->buffer_count - 1],
  1655. args->batch_len,
  1656. __func__,
  1657. ~0);
  1658. #endif
  1659. (void)i915_add_request(dev, flush_domains);
  1660. /* Exec the batchbuffer */
  1661. ret = i915_dispatch_gem_execbuffer(dev, args, exec_offset);
  1662. if (ret) {
  1663. DRM_ERROR("dispatch failed %d\n", ret);
  1664. goto err;
  1665. }
  1666. /*
  1667. * Ensure that the commands in the batch buffer are
  1668. * finished before the interrupt fires
  1669. */
  1670. flush_domains = i915_retire_commands(dev);
  1671. i915_verify_inactive(dev, __FILE__, __LINE__);
  1672. /*
  1673. * Get a seqno representing the execution of the current buffer,
  1674. * which we can wait on. We would like to mitigate these interrupts,
  1675. * likely by only creating seqnos occasionally (so that we have
  1676. * *some* interrupts representing completion of buffers that we can
  1677. * wait on when trying to clear up gtt space).
  1678. */
  1679. seqno = i915_add_request(dev, flush_domains);
  1680. BUG_ON(seqno == 0);
  1681. i915_file_priv->mm.last_gem_seqno = seqno;
  1682. for (i = 0; i < args->buffer_count; i++) {
  1683. struct drm_gem_object *obj = object_list[i];
  1684. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  1685. i915_gem_object_move_to_active(obj);
  1686. obj_priv->last_rendering_seqno = seqno;
  1687. #if WATCH_LRU
  1688. DRM_INFO("%s: move to exec list %p\n", __func__, obj);
  1689. #endif
  1690. }
  1691. #if WATCH_LRU
  1692. i915_dump_lru(dev, __func__);
  1693. #endif
  1694. i915_verify_inactive(dev, __FILE__, __LINE__);
  1695. /* Copy the new buffer offsets back to the user's exec list. */
  1696. ret = copy_to_user((struct drm_i915_relocation_entry __user *)
  1697. (uintptr_t) args->buffers_ptr,
  1698. exec_list,
  1699. sizeof(*exec_list) * args->buffer_count);
  1700. if (ret)
  1701. DRM_ERROR("failed to copy %d exec entries "
  1702. "back to user (%d)\n",
  1703. args->buffer_count, ret);
  1704. err:
  1705. if (object_list != NULL) {
  1706. for (i = 0; i < pinned; i++)
  1707. i915_gem_object_unpin(object_list[i]);
  1708. for (i = 0; i < args->buffer_count; i++)
  1709. drm_gem_object_unreference(object_list[i]);
  1710. }
  1711. mutex_unlock(&dev->struct_mutex);
  1712. pre_mutex_err:
  1713. drm_free(object_list, sizeof(*object_list) * args->buffer_count,
  1714. DRM_MEM_DRIVER);
  1715. drm_free(exec_list, sizeof(*exec_list) * args->buffer_count,
  1716. DRM_MEM_DRIVER);
  1717. return ret;
  1718. }
  1719. int
  1720. i915_gem_object_pin(struct drm_gem_object *obj, uint32_t alignment)
  1721. {
  1722. struct drm_device *dev = obj->dev;
  1723. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  1724. int ret;
  1725. i915_verify_inactive(dev, __FILE__, __LINE__);
  1726. if (obj_priv->gtt_space == NULL) {
  1727. ret = i915_gem_object_bind_to_gtt(obj, alignment);
  1728. if (ret != 0) {
  1729. DRM_ERROR("Failure to bind: %d", ret);
  1730. return ret;
  1731. }
  1732. }
  1733. obj_priv->pin_count++;
  1734. /* If the object is not active and not pending a flush,
  1735. * remove it from the inactive list
  1736. */
  1737. if (obj_priv->pin_count == 1) {
  1738. atomic_inc(&dev->pin_count);
  1739. atomic_add(obj->size, &dev->pin_memory);
  1740. if (!obj_priv->active &&
  1741. (obj->write_domain & ~(I915_GEM_DOMAIN_CPU |
  1742. I915_GEM_DOMAIN_GTT)) == 0 &&
  1743. !list_empty(&obj_priv->list))
  1744. list_del_init(&obj_priv->list);
  1745. }
  1746. i915_verify_inactive(dev, __FILE__, __LINE__);
  1747. return 0;
  1748. }
  1749. void
  1750. i915_gem_object_unpin(struct drm_gem_object *obj)
  1751. {
  1752. struct drm_device *dev = obj->dev;
  1753. drm_i915_private_t *dev_priv = dev->dev_private;
  1754. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  1755. i915_verify_inactive(dev, __FILE__, __LINE__);
  1756. obj_priv->pin_count--;
  1757. BUG_ON(obj_priv->pin_count < 0);
  1758. BUG_ON(obj_priv->gtt_space == NULL);
  1759. /* If the object is no longer pinned, and is
  1760. * neither active nor being flushed, then stick it on
  1761. * the inactive list
  1762. */
  1763. if (obj_priv->pin_count == 0) {
  1764. if (!obj_priv->active &&
  1765. (obj->write_domain & ~(I915_GEM_DOMAIN_CPU |
  1766. I915_GEM_DOMAIN_GTT)) == 0)
  1767. list_move_tail(&obj_priv->list,
  1768. &dev_priv->mm.inactive_list);
  1769. atomic_dec(&dev->pin_count);
  1770. atomic_sub(obj->size, &dev->pin_memory);
  1771. }
  1772. i915_verify_inactive(dev, __FILE__, __LINE__);
  1773. }
  1774. int
  1775. i915_gem_pin_ioctl(struct drm_device *dev, void *data,
  1776. struct drm_file *file_priv)
  1777. {
  1778. struct drm_i915_gem_pin *args = data;
  1779. struct drm_gem_object *obj;
  1780. struct drm_i915_gem_object *obj_priv;
  1781. int ret;
  1782. mutex_lock(&dev->struct_mutex);
  1783. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  1784. if (obj == NULL) {
  1785. DRM_ERROR("Bad handle in i915_gem_pin_ioctl(): %d\n",
  1786. args->handle);
  1787. mutex_unlock(&dev->struct_mutex);
  1788. return -EBADF;
  1789. }
  1790. obj_priv = obj->driver_private;
  1791. ret = i915_gem_object_pin(obj, args->alignment);
  1792. if (ret != 0) {
  1793. drm_gem_object_unreference(obj);
  1794. mutex_unlock(&dev->struct_mutex);
  1795. return ret;
  1796. }
  1797. /* XXX - flush the CPU caches for pinned objects
  1798. * as the X server doesn't manage domains yet
  1799. */
  1800. if (obj->write_domain & I915_GEM_DOMAIN_CPU) {
  1801. i915_gem_clflush_object(obj);
  1802. drm_agp_chipset_flush(dev);
  1803. obj->write_domain = 0;
  1804. }
  1805. args->offset = obj_priv->gtt_offset;
  1806. drm_gem_object_unreference(obj);
  1807. mutex_unlock(&dev->struct_mutex);
  1808. return 0;
  1809. }
  1810. int
  1811. i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
  1812. struct drm_file *file_priv)
  1813. {
  1814. struct drm_i915_gem_pin *args = data;
  1815. struct drm_gem_object *obj;
  1816. mutex_lock(&dev->struct_mutex);
  1817. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  1818. if (obj == NULL) {
  1819. DRM_ERROR("Bad handle in i915_gem_unpin_ioctl(): %d\n",
  1820. args->handle);
  1821. mutex_unlock(&dev->struct_mutex);
  1822. return -EBADF;
  1823. }
  1824. i915_gem_object_unpin(obj);
  1825. drm_gem_object_unreference(obj);
  1826. mutex_unlock(&dev->struct_mutex);
  1827. return 0;
  1828. }
  1829. int
  1830. i915_gem_busy_ioctl(struct drm_device *dev, void *data,
  1831. struct drm_file *file_priv)
  1832. {
  1833. struct drm_i915_gem_busy *args = data;
  1834. struct drm_gem_object *obj;
  1835. struct drm_i915_gem_object *obj_priv;
  1836. mutex_lock(&dev->struct_mutex);
  1837. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  1838. if (obj == NULL) {
  1839. DRM_ERROR("Bad handle in i915_gem_busy_ioctl(): %d\n",
  1840. args->handle);
  1841. mutex_unlock(&dev->struct_mutex);
  1842. return -EBADF;
  1843. }
  1844. obj_priv = obj->driver_private;
  1845. args->busy = obj_priv->active;
  1846. drm_gem_object_unreference(obj);
  1847. mutex_unlock(&dev->struct_mutex);
  1848. return 0;
  1849. }
  1850. int
  1851. i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
  1852. struct drm_file *file_priv)
  1853. {
  1854. return i915_gem_ring_throttle(dev, file_priv);
  1855. }
  1856. int i915_gem_init_object(struct drm_gem_object *obj)
  1857. {
  1858. struct drm_i915_gem_object *obj_priv;
  1859. obj_priv = drm_calloc(1, sizeof(*obj_priv), DRM_MEM_DRIVER);
  1860. if (obj_priv == NULL)
  1861. return -ENOMEM;
  1862. /*
  1863. * We've just allocated pages from the kernel,
  1864. * so they've just been written by the CPU with
  1865. * zeros. They'll need to be clflushed before we
  1866. * use them with the GPU.
  1867. */
  1868. obj->write_domain = I915_GEM_DOMAIN_CPU;
  1869. obj->read_domains = I915_GEM_DOMAIN_CPU;
  1870. obj->driver_private = obj_priv;
  1871. obj_priv->obj = obj;
  1872. INIT_LIST_HEAD(&obj_priv->list);
  1873. return 0;
  1874. }
  1875. void i915_gem_free_object(struct drm_gem_object *obj)
  1876. {
  1877. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  1878. while (obj_priv->pin_count > 0)
  1879. i915_gem_object_unpin(obj);
  1880. i915_gem_object_unbind(obj);
  1881. drm_free(obj_priv->page_cpu_valid, 1, DRM_MEM_DRIVER);
  1882. drm_free(obj->driver_private, 1, DRM_MEM_DRIVER);
  1883. }
  1884. static int
  1885. i915_gem_set_domain(struct drm_gem_object *obj,
  1886. struct drm_file *file_priv,
  1887. uint32_t read_domains,
  1888. uint32_t write_domain)
  1889. {
  1890. struct drm_device *dev = obj->dev;
  1891. int ret;
  1892. uint32_t flush_domains;
  1893. BUG_ON(!mutex_is_locked(&dev->struct_mutex));
  1894. ret = i915_gem_object_set_domain(obj, read_domains, write_domain);
  1895. if (ret)
  1896. return ret;
  1897. flush_domains = i915_gem_dev_set_domain(obj->dev);
  1898. if (flush_domains & ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT))
  1899. (void) i915_add_request(dev, flush_domains);
  1900. return 0;
  1901. }
  1902. /** Unbinds all objects that are on the given buffer list. */
  1903. static int
  1904. i915_gem_evict_from_list(struct drm_device *dev, struct list_head *head)
  1905. {
  1906. struct drm_gem_object *obj;
  1907. struct drm_i915_gem_object *obj_priv;
  1908. int ret;
  1909. while (!list_empty(head)) {
  1910. obj_priv = list_first_entry(head,
  1911. struct drm_i915_gem_object,
  1912. list);
  1913. obj = obj_priv->obj;
  1914. if (obj_priv->pin_count != 0) {
  1915. DRM_ERROR("Pinned object in unbind list\n");
  1916. mutex_unlock(&dev->struct_mutex);
  1917. return -EINVAL;
  1918. }
  1919. ret = i915_gem_object_unbind(obj);
  1920. if (ret != 0) {
  1921. DRM_ERROR("Error unbinding object in LeaveVT: %d\n",
  1922. ret);
  1923. mutex_unlock(&dev->struct_mutex);
  1924. return ret;
  1925. }
  1926. }
  1927. return 0;
  1928. }
  1929. static int
  1930. i915_gem_idle(struct drm_device *dev)
  1931. {
  1932. drm_i915_private_t *dev_priv = dev->dev_private;
  1933. uint32_t seqno, cur_seqno, last_seqno;
  1934. int stuck, ret;
  1935. if (dev_priv->mm.suspended)
  1936. return 0;
  1937. /* Hack! Don't let anybody do execbuf while we don't control the chip.
  1938. * We need to replace this with a semaphore, or something.
  1939. */
  1940. dev_priv->mm.suspended = 1;
  1941. i915_kernel_lost_context(dev);
  1942. /* Flush the GPU along with all non-CPU write domains
  1943. */
  1944. i915_gem_flush(dev, ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT),
  1945. ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT));
  1946. seqno = i915_add_request(dev, ~(I915_GEM_DOMAIN_CPU |
  1947. I915_GEM_DOMAIN_GTT));
  1948. if (seqno == 0) {
  1949. mutex_unlock(&dev->struct_mutex);
  1950. return -ENOMEM;
  1951. }
  1952. dev_priv->mm.waiting_gem_seqno = seqno;
  1953. last_seqno = 0;
  1954. stuck = 0;
  1955. for (;;) {
  1956. cur_seqno = i915_get_gem_seqno(dev);
  1957. if (i915_seqno_passed(cur_seqno, seqno))
  1958. break;
  1959. if (last_seqno == cur_seqno) {
  1960. if (stuck++ > 100) {
  1961. DRM_ERROR("hardware wedged\n");
  1962. dev_priv->mm.wedged = 1;
  1963. DRM_WAKEUP(&dev_priv->irq_queue);
  1964. break;
  1965. }
  1966. }
  1967. msleep(10);
  1968. last_seqno = cur_seqno;
  1969. }
  1970. dev_priv->mm.waiting_gem_seqno = 0;
  1971. i915_gem_retire_requests(dev);
  1972. /* Active and flushing should now be empty as we've
  1973. * waited for a sequence higher than any pending execbuffer
  1974. */
  1975. BUG_ON(!list_empty(&dev_priv->mm.active_list));
  1976. BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
  1977. /* Request should now be empty as we've also waited
  1978. * for the last request in the list
  1979. */
  1980. BUG_ON(!list_empty(&dev_priv->mm.request_list));
  1981. /* Move all buffers out of the GTT. */
  1982. ret = i915_gem_evict_from_list(dev, &dev_priv->mm.inactive_list);
  1983. if (ret)
  1984. return ret;
  1985. BUG_ON(!list_empty(&dev_priv->mm.active_list));
  1986. BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
  1987. BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
  1988. BUG_ON(!list_empty(&dev_priv->mm.request_list));
  1989. return 0;
  1990. }
  1991. static int
  1992. i915_gem_init_hws(struct drm_device *dev)
  1993. {
  1994. drm_i915_private_t *dev_priv = dev->dev_private;
  1995. struct drm_gem_object *obj;
  1996. struct drm_i915_gem_object *obj_priv;
  1997. int ret;
  1998. /* If we need a physical address for the status page, it's already
  1999. * initialized at driver load time.
  2000. */
  2001. if (!I915_NEED_GFX_HWS(dev))
  2002. return 0;
  2003. obj = drm_gem_object_alloc(dev, 4096);
  2004. if (obj == NULL) {
  2005. DRM_ERROR("Failed to allocate status page\n");
  2006. return -ENOMEM;
  2007. }
  2008. obj_priv = obj->driver_private;
  2009. ret = i915_gem_object_pin(obj, 4096);
  2010. if (ret != 0) {
  2011. drm_gem_object_unreference(obj);
  2012. return ret;
  2013. }
  2014. dev_priv->status_gfx_addr = obj_priv->gtt_offset;
  2015. dev_priv->hws_map.offset = dev->agp->base + obj_priv->gtt_offset;
  2016. dev_priv->hws_map.size = 4096;
  2017. dev_priv->hws_map.type = 0;
  2018. dev_priv->hws_map.flags = 0;
  2019. dev_priv->hws_map.mtrr = 0;
  2020. /* Ioremapping here is the wrong thing to do. We want cached access.
  2021. */
  2022. drm_core_ioremap_wc(&dev_priv->hws_map, dev);
  2023. if (dev_priv->hws_map.handle == NULL) {
  2024. DRM_ERROR("Failed to map status page.\n");
  2025. memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
  2026. drm_gem_object_unreference(obj);
  2027. return -EINVAL;
  2028. }
  2029. dev_priv->hws_obj = obj;
  2030. dev_priv->hw_status_page = dev_priv->hws_map.handle;
  2031. memset(dev_priv->hw_status_page, 0, PAGE_SIZE);
  2032. I915_WRITE(HWS_PGA, dev_priv->status_gfx_addr);
  2033. DRM_DEBUG("hws offset: 0x%08x\n", dev_priv->status_gfx_addr);
  2034. return 0;
  2035. }
  2036. static int
  2037. i915_gem_init_ringbuffer(struct drm_device *dev)
  2038. {
  2039. drm_i915_private_t *dev_priv = dev->dev_private;
  2040. struct drm_gem_object *obj;
  2041. struct drm_i915_gem_object *obj_priv;
  2042. int ret;
  2043. ret = i915_gem_init_hws(dev);
  2044. if (ret != 0)
  2045. return ret;
  2046. obj = drm_gem_object_alloc(dev, 128 * 1024);
  2047. if (obj == NULL) {
  2048. DRM_ERROR("Failed to allocate ringbuffer\n");
  2049. return -ENOMEM;
  2050. }
  2051. obj_priv = obj->driver_private;
  2052. ret = i915_gem_object_pin(obj, 4096);
  2053. if (ret != 0) {
  2054. drm_gem_object_unreference(obj);
  2055. return ret;
  2056. }
  2057. /* Set up the kernel mapping for the ring. */
  2058. dev_priv->ring.Size = obj->size;
  2059. dev_priv->ring.tail_mask = obj->size - 1;
  2060. dev_priv->ring.map.offset = dev->agp->base + obj_priv->gtt_offset;
  2061. dev_priv->ring.map.size = obj->size;
  2062. dev_priv->ring.map.type = 0;
  2063. dev_priv->ring.map.flags = 0;
  2064. dev_priv->ring.map.mtrr = 0;
  2065. drm_core_ioremap_wc(&dev_priv->ring.map, dev);
  2066. if (dev_priv->ring.map.handle == NULL) {
  2067. DRM_ERROR("Failed to map ringbuffer.\n");
  2068. memset(&dev_priv->ring, 0, sizeof(dev_priv->ring));
  2069. drm_gem_object_unreference(obj);
  2070. return -EINVAL;
  2071. }
  2072. dev_priv->ring.ring_obj = obj;
  2073. dev_priv->ring.virtual_start = dev_priv->ring.map.handle;
  2074. /* Stop the ring if it's running. */
  2075. I915_WRITE(PRB0_CTL, 0);
  2076. I915_WRITE(PRB0_HEAD, 0);
  2077. I915_WRITE(PRB0_TAIL, 0);
  2078. I915_WRITE(PRB0_START, 0);
  2079. /* Initialize the ring. */
  2080. I915_WRITE(PRB0_START, obj_priv->gtt_offset);
  2081. I915_WRITE(PRB0_CTL,
  2082. ((obj->size - 4096) & RING_NR_PAGES) |
  2083. RING_NO_REPORT |
  2084. RING_VALID);
  2085. /* Update our cache of the ring state */
  2086. i915_kernel_lost_context(dev);
  2087. return 0;
  2088. }
  2089. static void
  2090. i915_gem_cleanup_ringbuffer(struct drm_device *dev)
  2091. {
  2092. drm_i915_private_t *dev_priv = dev->dev_private;
  2093. if (dev_priv->ring.ring_obj == NULL)
  2094. return;
  2095. drm_core_ioremapfree(&dev_priv->ring.map, dev);
  2096. i915_gem_object_unpin(dev_priv->ring.ring_obj);
  2097. drm_gem_object_unreference(dev_priv->ring.ring_obj);
  2098. dev_priv->ring.ring_obj = NULL;
  2099. memset(&dev_priv->ring, 0, sizeof(dev_priv->ring));
  2100. if (dev_priv->hws_obj != NULL) {
  2101. i915_gem_object_unpin(dev_priv->hws_obj);
  2102. drm_gem_object_unreference(dev_priv->hws_obj);
  2103. dev_priv->hws_obj = NULL;
  2104. memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
  2105. /* Write high address into HWS_PGA when disabling. */
  2106. I915_WRITE(HWS_PGA, 0x1ffff000);
  2107. }
  2108. }
  2109. int
  2110. i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
  2111. struct drm_file *file_priv)
  2112. {
  2113. drm_i915_private_t *dev_priv = dev->dev_private;
  2114. int ret;
  2115. if (dev_priv->mm.wedged) {
  2116. DRM_ERROR("Reenabling wedged hardware, good luck\n");
  2117. dev_priv->mm.wedged = 0;
  2118. }
  2119. ret = i915_gem_init_ringbuffer(dev);
  2120. if (ret != 0)
  2121. return ret;
  2122. mutex_lock(&dev->struct_mutex);
  2123. BUG_ON(!list_empty(&dev_priv->mm.active_list));
  2124. BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
  2125. BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
  2126. BUG_ON(!list_empty(&dev_priv->mm.request_list));
  2127. dev_priv->mm.suspended = 0;
  2128. mutex_unlock(&dev->struct_mutex);
  2129. drm_irq_install(dev);
  2130. return 0;
  2131. }
  2132. int
  2133. i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
  2134. struct drm_file *file_priv)
  2135. {
  2136. int ret;
  2137. mutex_lock(&dev->struct_mutex);
  2138. ret = i915_gem_idle(dev);
  2139. if (ret == 0)
  2140. i915_gem_cleanup_ringbuffer(dev);
  2141. mutex_unlock(&dev->struct_mutex);
  2142. drm_irq_uninstall(dev);
  2143. return 0;
  2144. }
  2145. void
  2146. i915_gem_lastclose(struct drm_device *dev)
  2147. {
  2148. int ret;
  2149. drm_i915_private_t *dev_priv = dev->dev_private;
  2150. mutex_lock(&dev->struct_mutex);
  2151. if (dev_priv->ring.ring_obj != NULL) {
  2152. ret = i915_gem_idle(dev);
  2153. if (ret)
  2154. DRM_ERROR("failed to idle hardware: %d\n", ret);
  2155. i915_gem_cleanup_ringbuffer(dev);
  2156. }
  2157. mutex_unlock(&dev->struct_mutex);
  2158. }
  2159. void
  2160. i915_gem_load(struct drm_device *dev)
  2161. {
  2162. drm_i915_private_t *dev_priv = dev->dev_private;
  2163. INIT_LIST_HEAD(&dev_priv->mm.active_list);
  2164. INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
  2165. INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
  2166. INIT_LIST_HEAD(&dev_priv->mm.request_list);
  2167. INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
  2168. i915_gem_retire_work_handler);
  2169. INIT_WORK(&dev_priv->mm.vblank_work,
  2170. i915_gem_vblank_work_handler);
  2171. dev_priv->mm.next_gem_seqno = 1;
  2172. i915_gem_detect_bit_6_swizzle(dev);
  2173. }