i915_gem.c 66 KB

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