i915_gem.c 67 KB

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