i915_gem.c 100 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040
  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 "i915_trace.h"
  32. #include "intel_drv.h"
  33. #include <linux/shmem_fs.h>
  34. #include <linux/slab.h>
  35. #include <linux/swap.h>
  36. #include <linux/pci.h>
  37. #include <linux/dma-buf.h>
  38. static __must_check int i915_gem_object_flush_gpu_write_domain(struct drm_i915_gem_object *obj);
  39. static void i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj);
  40. static void i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj);
  41. static __must_check int i915_gem_object_bind_to_gtt(struct drm_i915_gem_object *obj,
  42. unsigned alignment,
  43. bool map_and_fenceable);
  44. static int i915_gem_phys_pwrite(struct drm_device *dev,
  45. struct drm_i915_gem_object *obj,
  46. struct drm_i915_gem_pwrite *args,
  47. struct drm_file *file);
  48. static void i915_gem_write_fence(struct drm_device *dev, int reg,
  49. struct drm_i915_gem_object *obj);
  50. static void i915_gem_object_update_fence(struct drm_i915_gem_object *obj,
  51. struct drm_i915_fence_reg *fence,
  52. bool enable);
  53. static int i915_gem_inactive_shrink(struct shrinker *shrinker,
  54. struct shrink_control *sc);
  55. static void i915_gem_object_truncate(struct drm_i915_gem_object *obj);
  56. static inline void i915_gem_object_fence_lost(struct drm_i915_gem_object *obj)
  57. {
  58. if (obj->tiling_mode)
  59. i915_gem_release_mmap(obj);
  60. /* As we do not have an associated fence register, we will force
  61. * a tiling change if we ever need to acquire one.
  62. */
  63. obj->fence_dirty = false;
  64. obj->fence_reg = I915_FENCE_REG_NONE;
  65. }
  66. /* some bookkeeping */
  67. static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv,
  68. size_t size)
  69. {
  70. dev_priv->mm.object_count++;
  71. dev_priv->mm.object_memory += size;
  72. }
  73. static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
  74. size_t size)
  75. {
  76. dev_priv->mm.object_count--;
  77. dev_priv->mm.object_memory -= size;
  78. }
  79. static int
  80. i915_gem_wait_for_error(struct drm_device *dev)
  81. {
  82. struct drm_i915_private *dev_priv = dev->dev_private;
  83. struct completion *x = &dev_priv->error_completion;
  84. unsigned long flags;
  85. int ret;
  86. if (!atomic_read(&dev_priv->mm.wedged))
  87. return 0;
  88. ret = wait_for_completion_interruptible(x);
  89. if (ret)
  90. return ret;
  91. if (atomic_read(&dev_priv->mm.wedged)) {
  92. /* GPU is hung, bump the completion count to account for
  93. * the token we just consumed so that we never hit zero and
  94. * end up waiting upon a subsequent completion event that
  95. * will never happen.
  96. */
  97. spin_lock_irqsave(&x->wait.lock, flags);
  98. x->done++;
  99. spin_unlock_irqrestore(&x->wait.lock, flags);
  100. }
  101. return 0;
  102. }
  103. int i915_mutex_lock_interruptible(struct drm_device *dev)
  104. {
  105. int ret;
  106. ret = i915_gem_wait_for_error(dev);
  107. if (ret)
  108. return ret;
  109. ret = mutex_lock_interruptible(&dev->struct_mutex);
  110. if (ret)
  111. return ret;
  112. WARN_ON(i915_verify_lists(dev));
  113. return 0;
  114. }
  115. static inline bool
  116. i915_gem_object_is_inactive(struct drm_i915_gem_object *obj)
  117. {
  118. return !obj->active;
  119. }
  120. int
  121. i915_gem_init_ioctl(struct drm_device *dev, void *data,
  122. struct drm_file *file)
  123. {
  124. struct drm_i915_gem_init *args = data;
  125. if (drm_core_check_feature(dev, DRIVER_MODESET))
  126. return -ENODEV;
  127. if (args->gtt_start >= args->gtt_end ||
  128. (args->gtt_end | args->gtt_start) & (PAGE_SIZE - 1))
  129. return -EINVAL;
  130. /* GEM with user mode setting was never supported on ilk and later. */
  131. if (INTEL_INFO(dev)->gen >= 5)
  132. return -ENODEV;
  133. mutex_lock(&dev->struct_mutex);
  134. i915_gem_init_global_gtt(dev, args->gtt_start,
  135. args->gtt_end, args->gtt_end);
  136. mutex_unlock(&dev->struct_mutex);
  137. return 0;
  138. }
  139. int
  140. i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
  141. struct drm_file *file)
  142. {
  143. struct drm_i915_private *dev_priv = dev->dev_private;
  144. struct drm_i915_gem_get_aperture *args = data;
  145. struct drm_i915_gem_object *obj;
  146. size_t pinned;
  147. pinned = 0;
  148. mutex_lock(&dev->struct_mutex);
  149. list_for_each_entry(obj, &dev_priv->mm.gtt_list, gtt_list)
  150. if (obj->pin_count)
  151. pinned += obj->gtt_space->size;
  152. mutex_unlock(&dev->struct_mutex);
  153. args->aper_size = dev_priv->mm.gtt_total;
  154. args->aper_available_size = args->aper_size - pinned;
  155. return 0;
  156. }
  157. static int
  158. i915_gem_create(struct drm_file *file,
  159. struct drm_device *dev,
  160. uint64_t size,
  161. uint32_t *handle_p)
  162. {
  163. struct drm_i915_gem_object *obj;
  164. int ret;
  165. u32 handle;
  166. size = roundup(size, PAGE_SIZE);
  167. if (size == 0)
  168. return -EINVAL;
  169. /* Allocate the new object */
  170. obj = i915_gem_alloc_object(dev, size);
  171. if (obj == NULL)
  172. return -ENOMEM;
  173. ret = drm_gem_handle_create(file, &obj->base, &handle);
  174. if (ret) {
  175. drm_gem_object_release(&obj->base);
  176. i915_gem_info_remove_obj(dev->dev_private, obj->base.size);
  177. kfree(obj);
  178. return ret;
  179. }
  180. /* drop reference from allocate - handle holds it now */
  181. drm_gem_object_unreference(&obj->base);
  182. trace_i915_gem_object_create(obj);
  183. *handle_p = handle;
  184. return 0;
  185. }
  186. int
  187. i915_gem_dumb_create(struct drm_file *file,
  188. struct drm_device *dev,
  189. struct drm_mode_create_dumb *args)
  190. {
  191. /* have to work out size/pitch and return them */
  192. args->pitch = ALIGN(args->width * ((args->bpp + 7) / 8), 64);
  193. args->size = args->pitch * args->height;
  194. return i915_gem_create(file, dev,
  195. args->size, &args->handle);
  196. }
  197. int i915_gem_dumb_destroy(struct drm_file *file,
  198. struct drm_device *dev,
  199. uint32_t handle)
  200. {
  201. return drm_gem_handle_delete(file, handle);
  202. }
  203. /**
  204. * Creates a new mm object and returns a handle to it.
  205. */
  206. int
  207. i915_gem_create_ioctl(struct drm_device *dev, void *data,
  208. struct drm_file *file)
  209. {
  210. struct drm_i915_gem_create *args = data;
  211. return i915_gem_create(file, dev,
  212. args->size, &args->handle);
  213. }
  214. static int i915_gem_object_needs_bit17_swizzle(struct drm_i915_gem_object *obj)
  215. {
  216. drm_i915_private_t *dev_priv = obj->base.dev->dev_private;
  217. return dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_9_10_17 &&
  218. obj->tiling_mode != I915_TILING_NONE;
  219. }
  220. static inline int
  221. __copy_to_user_swizzled(char __user *cpu_vaddr,
  222. const char *gpu_vaddr, int gpu_offset,
  223. int length)
  224. {
  225. int ret, cpu_offset = 0;
  226. while (length > 0) {
  227. int cacheline_end = ALIGN(gpu_offset + 1, 64);
  228. int this_length = min(cacheline_end - gpu_offset, length);
  229. int swizzled_gpu_offset = gpu_offset ^ 64;
  230. ret = __copy_to_user(cpu_vaddr + cpu_offset,
  231. gpu_vaddr + swizzled_gpu_offset,
  232. this_length);
  233. if (ret)
  234. return ret + length;
  235. cpu_offset += this_length;
  236. gpu_offset += this_length;
  237. length -= this_length;
  238. }
  239. return 0;
  240. }
  241. static inline int
  242. __copy_from_user_swizzled(char *gpu_vaddr, int gpu_offset,
  243. const char __user *cpu_vaddr,
  244. int length)
  245. {
  246. int ret, cpu_offset = 0;
  247. while (length > 0) {
  248. int cacheline_end = ALIGN(gpu_offset + 1, 64);
  249. int this_length = min(cacheline_end - gpu_offset, length);
  250. int swizzled_gpu_offset = gpu_offset ^ 64;
  251. ret = __copy_from_user(gpu_vaddr + swizzled_gpu_offset,
  252. cpu_vaddr + cpu_offset,
  253. this_length);
  254. if (ret)
  255. return ret + length;
  256. cpu_offset += this_length;
  257. gpu_offset += this_length;
  258. length -= this_length;
  259. }
  260. return 0;
  261. }
  262. /* Per-page copy function for the shmem pread fastpath.
  263. * Flushes invalid cachelines before reading the target if
  264. * needs_clflush is set. */
  265. static int
  266. shmem_pread_fast(struct page *page, int shmem_page_offset, int page_length,
  267. char __user *user_data,
  268. bool page_do_bit17_swizzling, bool needs_clflush)
  269. {
  270. char *vaddr;
  271. int ret;
  272. if (unlikely(page_do_bit17_swizzling))
  273. return -EINVAL;
  274. vaddr = kmap_atomic(page);
  275. if (needs_clflush)
  276. drm_clflush_virt_range(vaddr + shmem_page_offset,
  277. page_length);
  278. ret = __copy_to_user_inatomic(user_data,
  279. vaddr + shmem_page_offset,
  280. page_length);
  281. kunmap_atomic(vaddr);
  282. return ret;
  283. }
  284. static void
  285. shmem_clflush_swizzled_range(char *addr, unsigned long length,
  286. bool swizzled)
  287. {
  288. if (unlikely(swizzled)) {
  289. unsigned long start = (unsigned long) addr;
  290. unsigned long end = (unsigned long) addr + length;
  291. /* For swizzling simply ensure that we always flush both
  292. * channels. Lame, but simple and it works. Swizzled
  293. * pwrite/pread is far from a hotpath - current userspace
  294. * doesn't use it at all. */
  295. start = round_down(start, 128);
  296. end = round_up(end, 128);
  297. drm_clflush_virt_range((void *)start, end - start);
  298. } else {
  299. drm_clflush_virt_range(addr, length);
  300. }
  301. }
  302. /* Only difference to the fast-path function is that this can handle bit17
  303. * and uses non-atomic copy and kmap functions. */
  304. static int
  305. shmem_pread_slow(struct page *page, int shmem_page_offset, int page_length,
  306. char __user *user_data,
  307. bool page_do_bit17_swizzling, bool needs_clflush)
  308. {
  309. char *vaddr;
  310. int ret;
  311. vaddr = kmap(page);
  312. if (needs_clflush)
  313. shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
  314. page_length,
  315. page_do_bit17_swizzling);
  316. if (page_do_bit17_swizzling)
  317. ret = __copy_to_user_swizzled(user_data,
  318. vaddr, shmem_page_offset,
  319. page_length);
  320. else
  321. ret = __copy_to_user(user_data,
  322. vaddr + shmem_page_offset,
  323. page_length);
  324. kunmap(page);
  325. return ret;
  326. }
  327. static int
  328. i915_gem_shmem_pread(struct drm_device *dev,
  329. struct drm_i915_gem_object *obj,
  330. struct drm_i915_gem_pread *args,
  331. struct drm_file *file)
  332. {
  333. struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
  334. char __user *user_data;
  335. ssize_t remain;
  336. loff_t offset;
  337. int shmem_page_offset, page_length, ret = 0;
  338. int obj_do_bit17_swizzling, page_do_bit17_swizzling;
  339. int hit_slowpath = 0;
  340. int prefaulted = 0;
  341. int needs_clflush = 0;
  342. int release_page;
  343. user_data = (char __user *) (uintptr_t) args->data_ptr;
  344. remain = args->size;
  345. obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
  346. if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU)) {
  347. /* If we're not in the cpu read domain, set ourself into the gtt
  348. * read domain and manually flush cachelines (if required). This
  349. * optimizes for the case when the gpu will dirty the data
  350. * anyway again before the next pread happens. */
  351. if (obj->cache_level == I915_CACHE_NONE)
  352. needs_clflush = 1;
  353. ret = i915_gem_object_set_to_gtt_domain(obj, false);
  354. if (ret)
  355. return ret;
  356. }
  357. offset = args->offset;
  358. while (remain > 0) {
  359. struct page *page;
  360. /* Operation in this page
  361. *
  362. * shmem_page_offset = offset within page in shmem file
  363. * page_length = bytes to copy for this page
  364. */
  365. shmem_page_offset = offset_in_page(offset);
  366. page_length = remain;
  367. if ((shmem_page_offset + page_length) > PAGE_SIZE)
  368. page_length = PAGE_SIZE - shmem_page_offset;
  369. if (obj->pages) {
  370. page = obj->pages[offset >> PAGE_SHIFT];
  371. release_page = 0;
  372. } else {
  373. page = shmem_read_mapping_page(mapping, offset >> PAGE_SHIFT);
  374. if (IS_ERR(page)) {
  375. ret = PTR_ERR(page);
  376. goto out;
  377. }
  378. release_page = 1;
  379. }
  380. page_do_bit17_swizzling = obj_do_bit17_swizzling &&
  381. (page_to_phys(page) & (1 << 17)) != 0;
  382. ret = shmem_pread_fast(page, shmem_page_offset, page_length,
  383. user_data, page_do_bit17_swizzling,
  384. needs_clflush);
  385. if (ret == 0)
  386. goto next_page;
  387. hit_slowpath = 1;
  388. page_cache_get(page);
  389. mutex_unlock(&dev->struct_mutex);
  390. if (!prefaulted) {
  391. ret = fault_in_multipages_writeable(user_data, remain);
  392. /* Userspace is tricking us, but we've already clobbered
  393. * its pages with the prefault and promised to write the
  394. * data up to the first fault. Hence ignore any errors
  395. * and just continue. */
  396. (void)ret;
  397. prefaulted = 1;
  398. }
  399. ret = shmem_pread_slow(page, shmem_page_offset, page_length,
  400. user_data, page_do_bit17_swizzling,
  401. needs_clflush);
  402. mutex_lock(&dev->struct_mutex);
  403. page_cache_release(page);
  404. next_page:
  405. mark_page_accessed(page);
  406. if (release_page)
  407. page_cache_release(page);
  408. if (ret) {
  409. ret = -EFAULT;
  410. goto out;
  411. }
  412. remain -= page_length;
  413. user_data += page_length;
  414. offset += page_length;
  415. }
  416. out:
  417. if (hit_slowpath) {
  418. /* Fixup: Kill any reinstated backing storage pages */
  419. if (obj->madv == __I915_MADV_PURGED)
  420. i915_gem_object_truncate(obj);
  421. }
  422. return ret;
  423. }
  424. /**
  425. * Reads data from the object referenced by handle.
  426. *
  427. * On error, the contents of *data are undefined.
  428. */
  429. int
  430. i915_gem_pread_ioctl(struct drm_device *dev, void *data,
  431. struct drm_file *file)
  432. {
  433. struct drm_i915_gem_pread *args = data;
  434. struct drm_i915_gem_object *obj;
  435. int ret = 0;
  436. if (args->size == 0)
  437. return 0;
  438. if (!access_ok(VERIFY_WRITE,
  439. (char __user *)(uintptr_t)args->data_ptr,
  440. args->size))
  441. return -EFAULT;
  442. ret = i915_mutex_lock_interruptible(dev);
  443. if (ret)
  444. return ret;
  445. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  446. if (&obj->base == NULL) {
  447. ret = -ENOENT;
  448. goto unlock;
  449. }
  450. /* Bounds check source. */
  451. if (args->offset > obj->base.size ||
  452. args->size > obj->base.size - args->offset) {
  453. ret = -EINVAL;
  454. goto out;
  455. }
  456. /* prime objects have no backing filp to GEM pread/pwrite
  457. * pages from.
  458. */
  459. if (!obj->base.filp) {
  460. ret = -EINVAL;
  461. goto out;
  462. }
  463. trace_i915_gem_object_pread(obj, args->offset, args->size);
  464. ret = i915_gem_shmem_pread(dev, obj, args, file);
  465. out:
  466. drm_gem_object_unreference(&obj->base);
  467. unlock:
  468. mutex_unlock(&dev->struct_mutex);
  469. return ret;
  470. }
  471. /* This is the fast write path which cannot handle
  472. * page faults in the source data
  473. */
  474. static inline int
  475. fast_user_write(struct io_mapping *mapping,
  476. loff_t page_base, int page_offset,
  477. char __user *user_data,
  478. int length)
  479. {
  480. void __iomem *vaddr_atomic;
  481. void *vaddr;
  482. unsigned long unwritten;
  483. vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
  484. /* We can use the cpu mem copy function because this is X86. */
  485. vaddr = (void __force*)vaddr_atomic + page_offset;
  486. unwritten = __copy_from_user_inatomic_nocache(vaddr,
  487. user_data, length);
  488. io_mapping_unmap_atomic(vaddr_atomic);
  489. return unwritten;
  490. }
  491. /**
  492. * This is the fast pwrite path, where we copy the data directly from the
  493. * user into the GTT, uncached.
  494. */
  495. static int
  496. i915_gem_gtt_pwrite_fast(struct drm_device *dev,
  497. struct drm_i915_gem_object *obj,
  498. struct drm_i915_gem_pwrite *args,
  499. struct drm_file *file)
  500. {
  501. drm_i915_private_t *dev_priv = dev->dev_private;
  502. ssize_t remain;
  503. loff_t offset, page_base;
  504. char __user *user_data;
  505. int page_offset, page_length, ret;
  506. ret = i915_gem_object_pin(obj, 0, true);
  507. if (ret)
  508. goto out;
  509. ret = i915_gem_object_set_to_gtt_domain(obj, true);
  510. if (ret)
  511. goto out_unpin;
  512. ret = i915_gem_object_put_fence(obj);
  513. if (ret)
  514. goto out_unpin;
  515. user_data = (char __user *) (uintptr_t) args->data_ptr;
  516. remain = args->size;
  517. offset = obj->gtt_offset + args->offset;
  518. while (remain > 0) {
  519. /* Operation in this page
  520. *
  521. * page_base = page offset within aperture
  522. * page_offset = offset within page
  523. * page_length = bytes to copy for this page
  524. */
  525. page_base = offset & PAGE_MASK;
  526. page_offset = offset_in_page(offset);
  527. page_length = remain;
  528. if ((page_offset + remain) > PAGE_SIZE)
  529. page_length = PAGE_SIZE - page_offset;
  530. /* If we get a fault while copying data, then (presumably) our
  531. * source page isn't available. Return the error and we'll
  532. * retry in the slow path.
  533. */
  534. if (fast_user_write(dev_priv->mm.gtt_mapping, page_base,
  535. page_offset, user_data, page_length)) {
  536. ret = -EFAULT;
  537. goto out_unpin;
  538. }
  539. remain -= page_length;
  540. user_data += page_length;
  541. offset += page_length;
  542. }
  543. out_unpin:
  544. i915_gem_object_unpin(obj);
  545. out:
  546. return ret;
  547. }
  548. /* Per-page copy function for the shmem pwrite fastpath.
  549. * Flushes invalid cachelines before writing to the target if
  550. * needs_clflush_before is set and flushes out any written cachelines after
  551. * writing if needs_clflush is set. */
  552. static int
  553. shmem_pwrite_fast(struct page *page, int shmem_page_offset, int page_length,
  554. char __user *user_data,
  555. bool page_do_bit17_swizzling,
  556. bool needs_clflush_before,
  557. bool needs_clflush_after)
  558. {
  559. char *vaddr;
  560. int ret;
  561. if (unlikely(page_do_bit17_swizzling))
  562. return -EINVAL;
  563. vaddr = kmap_atomic(page);
  564. if (needs_clflush_before)
  565. drm_clflush_virt_range(vaddr + shmem_page_offset,
  566. page_length);
  567. ret = __copy_from_user_inatomic_nocache(vaddr + shmem_page_offset,
  568. user_data,
  569. page_length);
  570. if (needs_clflush_after)
  571. drm_clflush_virt_range(vaddr + shmem_page_offset,
  572. page_length);
  573. kunmap_atomic(vaddr);
  574. return ret;
  575. }
  576. /* Only difference to the fast-path function is that this can handle bit17
  577. * and uses non-atomic copy and kmap functions. */
  578. static int
  579. shmem_pwrite_slow(struct page *page, int shmem_page_offset, int page_length,
  580. char __user *user_data,
  581. bool page_do_bit17_swizzling,
  582. bool needs_clflush_before,
  583. bool needs_clflush_after)
  584. {
  585. char *vaddr;
  586. int ret;
  587. vaddr = kmap(page);
  588. if (unlikely(needs_clflush_before || page_do_bit17_swizzling))
  589. shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
  590. page_length,
  591. page_do_bit17_swizzling);
  592. if (page_do_bit17_swizzling)
  593. ret = __copy_from_user_swizzled(vaddr, shmem_page_offset,
  594. user_data,
  595. page_length);
  596. else
  597. ret = __copy_from_user(vaddr + shmem_page_offset,
  598. user_data,
  599. page_length);
  600. if (needs_clflush_after)
  601. shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
  602. page_length,
  603. page_do_bit17_swizzling);
  604. kunmap(page);
  605. return ret;
  606. }
  607. static int
  608. i915_gem_shmem_pwrite(struct drm_device *dev,
  609. struct drm_i915_gem_object *obj,
  610. struct drm_i915_gem_pwrite *args,
  611. struct drm_file *file)
  612. {
  613. struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
  614. ssize_t remain;
  615. loff_t offset;
  616. char __user *user_data;
  617. int shmem_page_offset, page_length, ret = 0;
  618. int obj_do_bit17_swizzling, page_do_bit17_swizzling;
  619. int hit_slowpath = 0;
  620. int needs_clflush_after = 0;
  621. int needs_clflush_before = 0;
  622. int release_page;
  623. user_data = (char __user *) (uintptr_t) args->data_ptr;
  624. remain = args->size;
  625. obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
  626. if (obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
  627. /* If we're not in the cpu write domain, set ourself into the gtt
  628. * write domain and manually flush cachelines (if required). This
  629. * optimizes for the case when the gpu will use the data
  630. * right away and we therefore have to clflush anyway. */
  631. if (obj->cache_level == I915_CACHE_NONE)
  632. needs_clflush_after = 1;
  633. ret = i915_gem_object_set_to_gtt_domain(obj, true);
  634. if (ret)
  635. return ret;
  636. }
  637. /* Same trick applies for invalidate partially written cachelines before
  638. * writing. */
  639. if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU)
  640. && obj->cache_level == I915_CACHE_NONE)
  641. needs_clflush_before = 1;
  642. offset = args->offset;
  643. obj->dirty = 1;
  644. while (remain > 0) {
  645. struct page *page;
  646. int partial_cacheline_write;
  647. /* Operation in this page
  648. *
  649. * shmem_page_offset = offset within page in shmem file
  650. * page_length = bytes to copy for this page
  651. */
  652. shmem_page_offset = offset_in_page(offset);
  653. page_length = remain;
  654. if ((shmem_page_offset + page_length) > PAGE_SIZE)
  655. page_length = PAGE_SIZE - shmem_page_offset;
  656. /* If we don't overwrite a cacheline completely we need to be
  657. * careful to have up-to-date data by first clflushing. Don't
  658. * overcomplicate things and flush the entire patch. */
  659. partial_cacheline_write = needs_clflush_before &&
  660. ((shmem_page_offset | page_length)
  661. & (boot_cpu_data.x86_clflush_size - 1));
  662. if (obj->pages) {
  663. page = obj->pages[offset >> PAGE_SHIFT];
  664. release_page = 0;
  665. } else {
  666. page = shmem_read_mapping_page(mapping, offset >> PAGE_SHIFT);
  667. if (IS_ERR(page)) {
  668. ret = PTR_ERR(page);
  669. goto out;
  670. }
  671. release_page = 1;
  672. }
  673. page_do_bit17_swizzling = obj_do_bit17_swizzling &&
  674. (page_to_phys(page) & (1 << 17)) != 0;
  675. ret = shmem_pwrite_fast(page, shmem_page_offset, page_length,
  676. user_data, page_do_bit17_swizzling,
  677. partial_cacheline_write,
  678. needs_clflush_after);
  679. if (ret == 0)
  680. goto next_page;
  681. hit_slowpath = 1;
  682. page_cache_get(page);
  683. mutex_unlock(&dev->struct_mutex);
  684. ret = shmem_pwrite_slow(page, shmem_page_offset, page_length,
  685. user_data, page_do_bit17_swizzling,
  686. partial_cacheline_write,
  687. needs_clflush_after);
  688. mutex_lock(&dev->struct_mutex);
  689. page_cache_release(page);
  690. next_page:
  691. set_page_dirty(page);
  692. mark_page_accessed(page);
  693. if (release_page)
  694. page_cache_release(page);
  695. if (ret) {
  696. ret = -EFAULT;
  697. goto out;
  698. }
  699. remain -= page_length;
  700. user_data += page_length;
  701. offset += page_length;
  702. }
  703. out:
  704. if (hit_slowpath) {
  705. /* Fixup: Kill any reinstated backing storage pages */
  706. if (obj->madv == __I915_MADV_PURGED)
  707. i915_gem_object_truncate(obj);
  708. /* and flush dirty cachelines in case the object isn't in the cpu write
  709. * domain anymore. */
  710. if (obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
  711. i915_gem_clflush_object(obj);
  712. intel_gtt_chipset_flush();
  713. }
  714. }
  715. if (needs_clflush_after)
  716. intel_gtt_chipset_flush();
  717. return ret;
  718. }
  719. /**
  720. * Writes data to the object referenced by handle.
  721. *
  722. * On error, the contents of the buffer that were to be modified are undefined.
  723. */
  724. int
  725. i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
  726. struct drm_file *file)
  727. {
  728. struct drm_i915_gem_pwrite *args = data;
  729. struct drm_i915_gem_object *obj;
  730. int ret;
  731. if (args->size == 0)
  732. return 0;
  733. if (!access_ok(VERIFY_READ,
  734. (char __user *)(uintptr_t)args->data_ptr,
  735. args->size))
  736. return -EFAULT;
  737. ret = fault_in_multipages_readable((char __user *)(uintptr_t)args->data_ptr,
  738. args->size);
  739. if (ret)
  740. return -EFAULT;
  741. ret = i915_mutex_lock_interruptible(dev);
  742. if (ret)
  743. return ret;
  744. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  745. if (&obj->base == NULL) {
  746. ret = -ENOENT;
  747. goto unlock;
  748. }
  749. /* Bounds check destination. */
  750. if (args->offset > obj->base.size ||
  751. args->size > obj->base.size - args->offset) {
  752. ret = -EINVAL;
  753. goto out;
  754. }
  755. /* prime objects have no backing filp to GEM pread/pwrite
  756. * pages from.
  757. */
  758. if (!obj->base.filp) {
  759. ret = -EINVAL;
  760. goto out;
  761. }
  762. trace_i915_gem_object_pwrite(obj, args->offset, args->size);
  763. ret = -EFAULT;
  764. /* We can only do the GTT pwrite on untiled buffers, as otherwise
  765. * it would end up going through the fenced access, and we'll get
  766. * different detiling behavior between reading and writing.
  767. * pread/pwrite currently are reading and writing from the CPU
  768. * perspective, requiring manual detiling by the client.
  769. */
  770. if (obj->phys_obj) {
  771. ret = i915_gem_phys_pwrite(dev, obj, args, file);
  772. goto out;
  773. }
  774. if (obj->gtt_space &&
  775. obj->cache_level == I915_CACHE_NONE &&
  776. obj->tiling_mode == I915_TILING_NONE &&
  777. obj->map_and_fenceable &&
  778. obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
  779. ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file);
  780. /* Note that the gtt paths might fail with non-page-backed user
  781. * pointers (e.g. gtt mappings when moving data between
  782. * textures). Fallback to the shmem path in that case. */
  783. }
  784. if (ret == -EFAULT)
  785. ret = i915_gem_shmem_pwrite(dev, obj, args, file);
  786. out:
  787. drm_gem_object_unreference(&obj->base);
  788. unlock:
  789. mutex_unlock(&dev->struct_mutex);
  790. return ret;
  791. }
  792. /**
  793. * Called when user space prepares to use an object with the CPU, either
  794. * through the mmap ioctl's mapping or a GTT mapping.
  795. */
  796. int
  797. i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
  798. struct drm_file *file)
  799. {
  800. struct drm_i915_gem_set_domain *args = data;
  801. struct drm_i915_gem_object *obj;
  802. uint32_t read_domains = args->read_domains;
  803. uint32_t write_domain = args->write_domain;
  804. int ret;
  805. /* Only handle setting domains to types used by the CPU. */
  806. if (write_domain & I915_GEM_GPU_DOMAINS)
  807. return -EINVAL;
  808. if (read_domains & I915_GEM_GPU_DOMAINS)
  809. return -EINVAL;
  810. /* Having something in the write domain implies it's in the read
  811. * domain, and only that read domain. Enforce that in the request.
  812. */
  813. if (write_domain != 0 && read_domains != write_domain)
  814. return -EINVAL;
  815. ret = i915_mutex_lock_interruptible(dev);
  816. if (ret)
  817. return ret;
  818. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  819. if (&obj->base == NULL) {
  820. ret = -ENOENT;
  821. goto unlock;
  822. }
  823. if (read_domains & I915_GEM_DOMAIN_GTT) {
  824. ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
  825. /* Silently promote "you're not bound, there was nothing to do"
  826. * to success, since the client was just asking us to
  827. * make sure everything was done.
  828. */
  829. if (ret == -EINVAL)
  830. ret = 0;
  831. } else {
  832. ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
  833. }
  834. drm_gem_object_unreference(&obj->base);
  835. unlock:
  836. mutex_unlock(&dev->struct_mutex);
  837. return ret;
  838. }
  839. /**
  840. * Called when user space has done writes to this buffer
  841. */
  842. int
  843. i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
  844. struct drm_file *file)
  845. {
  846. struct drm_i915_gem_sw_finish *args = data;
  847. struct drm_i915_gem_object *obj;
  848. int ret = 0;
  849. ret = i915_mutex_lock_interruptible(dev);
  850. if (ret)
  851. return ret;
  852. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  853. if (&obj->base == NULL) {
  854. ret = -ENOENT;
  855. goto unlock;
  856. }
  857. /* Pinned buffers may be scanout, so flush the cache */
  858. if (obj->pin_count)
  859. i915_gem_object_flush_cpu_write_domain(obj);
  860. drm_gem_object_unreference(&obj->base);
  861. unlock:
  862. mutex_unlock(&dev->struct_mutex);
  863. return ret;
  864. }
  865. /**
  866. * Maps the contents of an object, returning the address it is mapped
  867. * into.
  868. *
  869. * While the mapping holds a reference on the contents of the object, it doesn't
  870. * imply a ref on the object itself.
  871. */
  872. int
  873. i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
  874. struct drm_file *file)
  875. {
  876. struct drm_i915_gem_mmap *args = data;
  877. struct drm_gem_object *obj;
  878. unsigned long addr;
  879. obj = drm_gem_object_lookup(dev, file, args->handle);
  880. if (obj == NULL)
  881. return -ENOENT;
  882. /* prime objects have no backing filp to GEM mmap
  883. * pages from.
  884. */
  885. if (!obj->filp) {
  886. drm_gem_object_unreference_unlocked(obj);
  887. return -EINVAL;
  888. }
  889. addr = vm_mmap(obj->filp, 0, args->size,
  890. PROT_READ | PROT_WRITE, MAP_SHARED,
  891. args->offset);
  892. drm_gem_object_unreference_unlocked(obj);
  893. if (IS_ERR((void *)addr))
  894. return addr;
  895. args->addr_ptr = (uint64_t) addr;
  896. return 0;
  897. }
  898. /**
  899. * i915_gem_fault - fault a page into the GTT
  900. * vma: VMA in question
  901. * vmf: fault info
  902. *
  903. * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
  904. * from userspace. The fault handler takes care of binding the object to
  905. * the GTT (if needed), allocating and programming a fence register (again,
  906. * only if needed based on whether the old reg is still valid or the object
  907. * is tiled) and inserting a new PTE into the faulting process.
  908. *
  909. * Note that the faulting process may involve evicting existing objects
  910. * from the GTT and/or fence registers to make room. So performance may
  911. * suffer if the GTT working set is large or there are few fence registers
  912. * left.
  913. */
  914. int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  915. {
  916. struct drm_i915_gem_object *obj = to_intel_bo(vma->vm_private_data);
  917. struct drm_device *dev = obj->base.dev;
  918. drm_i915_private_t *dev_priv = dev->dev_private;
  919. pgoff_t page_offset;
  920. unsigned long pfn;
  921. int ret = 0;
  922. bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
  923. /* We don't use vmf->pgoff since that has the fake offset */
  924. page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
  925. PAGE_SHIFT;
  926. ret = i915_mutex_lock_interruptible(dev);
  927. if (ret)
  928. goto out;
  929. trace_i915_gem_object_fault(obj, page_offset, true, write);
  930. /* Now bind it into the GTT if needed */
  931. if (!obj->map_and_fenceable) {
  932. ret = i915_gem_object_unbind(obj);
  933. if (ret)
  934. goto unlock;
  935. }
  936. if (!obj->gtt_space) {
  937. ret = i915_gem_object_bind_to_gtt(obj, 0, true);
  938. if (ret)
  939. goto unlock;
  940. ret = i915_gem_object_set_to_gtt_domain(obj, write);
  941. if (ret)
  942. goto unlock;
  943. }
  944. if (!obj->has_global_gtt_mapping)
  945. i915_gem_gtt_bind_object(obj, obj->cache_level);
  946. ret = i915_gem_object_get_fence(obj);
  947. if (ret)
  948. goto unlock;
  949. if (i915_gem_object_is_inactive(obj))
  950. list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
  951. obj->fault_mappable = true;
  952. pfn = ((dev->agp->base + obj->gtt_offset) >> PAGE_SHIFT) +
  953. page_offset;
  954. /* Finally, remap it using the new GTT offset */
  955. ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
  956. unlock:
  957. mutex_unlock(&dev->struct_mutex);
  958. out:
  959. switch (ret) {
  960. case -EIO:
  961. case -EAGAIN:
  962. /* Give the error handler a chance to run and move the
  963. * objects off the GPU active list. Next time we service the
  964. * fault, we should be able to transition the page into the
  965. * GTT without touching the GPU (and so avoid further
  966. * EIO/EGAIN). If the GPU is wedged, then there is no issue
  967. * with coherency, just lost writes.
  968. */
  969. set_need_resched();
  970. case 0:
  971. case -ERESTARTSYS:
  972. case -EINTR:
  973. return VM_FAULT_NOPAGE;
  974. case -ENOMEM:
  975. return VM_FAULT_OOM;
  976. default:
  977. return VM_FAULT_SIGBUS;
  978. }
  979. }
  980. /**
  981. * i915_gem_release_mmap - remove physical page mappings
  982. * @obj: obj in question
  983. *
  984. * Preserve the reservation of the mmapping with the DRM core code, but
  985. * relinquish ownership of the pages back to the system.
  986. *
  987. * It is vital that we remove the page mapping if we have mapped a tiled
  988. * object through the GTT and then lose the fence register due to
  989. * resource pressure. Similarly if the object has been moved out of the
  990. * aperture, than pages mapped into userspace must be revoked. Removing the
  991. * mapping will then trigger a page fault on the next user access, allowing
  992. * fixup by i915_gem_fault().
  993. */
  994. void
  995. i915_gem_release_mmap(struct drm_i915_gem_object *obj)
  996. {
  997. if (!obj->fault_mappable)
  998. return;
  999. if (obj->base.dev->dev_mapping)
  1000. unmap_mapping_range(obj->base.dev->dev_mapping,
  1001. (loff_t)obj->base.map_list.hash.key<<PAGE_SHIFT,
  1002. obj->base.size, 1);
  1003. obj->fault_mappable = false;
  1004. }
  1005. static uint32_t
  1006. i915_gem_get_gtt_size(struct drm_device *dev, uint32_t size, int tiling_mode)
  1007. {
  1008. uint32_t gtt_size;
  1009. if (INTEL_INFO(dev)->gen >= 4 ||
  1010. tiling_mode == I915_TILING_NONE)
  1011. return size;
  1012. /* Previous chips need a power-of-two fence region when tiling */
  1013. if (INTEL_INFO(dev)->gen == 3)
  1014. gtt_size = 1024*1024;
  1015. else
  1016. gtt_size = 512*1024;
  1017. while (gtt_size < size)
  1018. gtt_size <<= 1;
  1019. return gtt_size;
  1020. }
  1021. /**
  1022. * i915_gem_get_gtt_alignment - return required GTT alignment for an object
  1023. * @obj: object to check
  1024. *
  1025. * Return the required GTT alignment for an object, taking into account
  1026. * potential fence register mapping.
  1027. */
  1028. static uint32_t
  1029. i915_gem_get_gtt_alignment(struct drm_device *dev,
  1030. uint32_t size,
  1031. int tiling_mode)
  1032. {
  1033. /*
  1034. * Minimum alignment is 4k (GTT page size), but might be greater
  1035. * if a fence register is needed for the object.
  1036. */
  1037. if (INTEL_INFO(dev)->gen >= 4 ||
  1038. tiling_mode == I915_TILING_NONE)
  1039. return 4096;
  1040. /*
  1041. * Previous chips need to be aligned to the size of the smallest
  1042. * fence register that can contain the object.
  1043. */
  1044. return i915_gem_get_gtt_size(dev, size, tiling_mode);
  1045. }
  1046. /**
  1047. * i915_gem_get_unfenced_gtt_alignment - return required GTT alignment for an
  1048. * unfenced object
  1049. * @dev: the device
  1050. * @size: size of the object
  1051. * @tiling_mode: tiling mode of the object
  1052. *
  1053. * Return the required GTT alignment for an object, only taking into account
  1054. * unfenced tiled surface requirements.
  1055. */
  1056. uint32_t
  1057. i915_gem_get_unfenced_gtt_alignment(struct drm_device *dev,
  1058. uint32_t size,
  1059. int tiling_mode)
  1060. {
  1061. /*
  1062. * Minimum alignment is 4k (GTT page size) for sane hw.
  1063. */
  1064. if (INTEL_INFO(dev)->gen >= 4 || IS_G33(dev) ||
  1065. tiling_mode == I915_TILING_NONE)
  1066. return 4096;
  1067. /* Previous hardware however needs to be aligned to a power-of-two
  1068. * tile height. The simplest method for determining this is to reuse
  1069. * the power-of-tile object size.
  1070. */
  1071. return i915_gem_get_gtt_size(dev, size, tiling_mode);
  1072. }
  1073. int
  1074. i915_gem_mmap_gtt(struct drm_file *file,
  1075. struct drm_device *dev,
  1076. uint32_t handle,
  1077. uint64_t *offset)
  1078. {
  1079. struct drm_i915_private *dev_priv = dev->dev_private;
  1080. struct drm_i915_gem_object *obj;
  1081. int ret;
  1082. ret = i915_mutex_lock_interruptible(dev);
  1083. if (ret)
  1084. return ret;
  1085. obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
  1086. if (&obj->base == NULL) {
  1087. ret = -ENOENT;
  1088. goto unlock;
  1089. }
  1090. if (obj->base.size > dev_priv->mm.gtt_mappable_end) {
  1091. ret = -E2BIG;
  1092. goto out;
  1093. }
  1094. if (obj->madv != I915_MADV_WILLNEED) {
  1095. DRM_ERROR("Attempting to mmap a purgeable buffer\n");
  1096. ret = -EINVAL;
  1097. goto out;
  1098. }
  1099. if (!obj->base.map_list.map) {
  1100. ret = drm_gem_create_mmap_offset(&obj->base);
  1101. if (ret)
  1102. goto out;
  1103. }
  1104. *offset = (u64)obj->base.map_list.hash.key << PAGE_SHIFT;
  1105. out:
  1106. drm_gem_object_unreference(&obj->base);
  1107. unlock:
  1108. mutex_unlock(&dev->struct_mutex);
  1109. return ret;
  1110. }
  1111. /**
  1112. * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
  1113. * @dev: DRM device
  1114. * @data: GTT mapping ioctl data
  1115. * @file: GEM object info
  1116. *
  1117. * Simply returns the fake offset to userspace so it can mmap it.
  1118. * The mmap call will end up in drm_gem_mmap(), which will set things
  1119. * up so we can get faults in the handler above.
  1120. *
  1121. * The fault handler will take care of binding the object into the GTT
  1122. * (since it may have been evicted to make room for something), allocating
  1123. * a fence register, and mapping the appropriate aperture address into
  1124. * userspace.
  1125. */
  1126. int
  1127. i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
  1128. struct drm_file *file)
  1129. {
  1130. struct drm_i915_gem_mmap_gtt *args = data;
  1131. return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset);
  1132. }
  1133. int
  1134. i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj,
  1135. gfp_t gfpmask)
  1136. {
  1137. int page_count, i;
  1138. struct address_space *mapping;
  1139. struct inode *inode;
  1140. struct page *page;
  1141. if (obj->pages || obj->sg_table)
  1142. return 0;
  1143. /* Get the list of pages out of our struct file. They'll be pinned
  1144. * at this point until we release them.
  1145. */
  1146. page_count = obj->base.size / PAGE_SIZE;
  1147. BUG_ON(obj->pages != NULL);
  1148. obj->pages = drm_malloc_ab(page_count, sizeof(struct page *));
  1149. if (obj->pages == NULL)
  1150. return -ENOMEM;
  1151. inode = obj->base.filp->f_path.dentry->d_inode;
  1152. mapping = inode->i_mapping;
  1153. gfpmask |= mapping_gfp_mask(mapping);
  1154. for (i = 0; i < page_count; i++) {
  1155. page = shmem_read_mapping_page_gfp(mapping, i, gfpmask);
  1156. if (IS_ERR(page))
  1157. goto err_pages;
  1158. obj->pages[i] = page;
  1159. }
  1160. if (i915_gem_object_needs_bit17_swizzle(obj))
  1161. i915_gem_object_do_bit_17_swizzle(obj);
  1162. return 0;
  1163. err_pages:
  1164. while (i--)
  1165. page_cache_release(obj->pages[i]);
  1166. drm_free_large(obj->pages);
  1167. obj->pages = NULL;
  1168. return PTR_ERR(page);
  1169. }
  1170. static void
  1171. i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj)
  1172. {
  1173. int page_count = obj->base.size / PAGE_SIZE;
  1174. int i;
  1175. if (!obj->pages)
  1176. return;
  1177. BUG_ON(obj->madv == __I915_MADV_PURGED);
  1178. if (i915_gem_object_needs_bit17_swizzle(obj))
  1179. i915_gem_object_save_bit_17_swizzle(obj);
  1180. if (obj->madv == I915_MADV_DONTNEED)
  1181. obj->dirty = 0;
  1182. for (i = 0; i < page_count; i++) {
  1183. if (obj->dirty)
  1184. set_page_dirty(obj->pages[i]);
  1185. if (obj->madv == I915_MADV_WILLNEED)
  1186. mark_page_accessed(obj->pages[i]);
  1187. page_cache_release(obj->pages[i]);
  1188. }
  1189. obj->dirty = 0;
  1190. drm_free_large(obj->pages);
  1191. obj->pages = NULL;
  1192. }
  1193. void
  1194. i915_gem_object_move_to_active(struct drm_i915_gem_object *obj,
  1195. struct intel_ring_buffer *ring,
  1196. u32 seqno)
  1197. {
  1198. struct drm_device *dev = obj->base.dev;
  1199. struct drm_i915_private *dev_priv = dev->dev_private;
  1200. BUG_ON(ring == NULL);
  1201. obj->ring = ring;
  1202. /* Add a reference if we're newly entering the active list. */
  1203. if (!obj->active) {
  1204. drm_gem_object_reference(&obj->base);
  1205. obj->active = 1;
  1206. }
  1207. /* Move from whatever list we were on to the tail of execution. */
  1208. list_move_tail(&obj->mm_list, &dev_priv->mm.active_list);
  1209. list_move_tail(&obj->ring_list, &ring->active_list);
  1210. obj->last_rendering_seqno = seqno;
  1211. if (obj->fenced_gpu_access) {
  1212. obj->last_fenced_seqno = seqno;
  1213. /* Bump MRU to take account of the delayed flush */
  1214. if (obj->fence_reg != I915_FENCE_REG_NONE) {
  1215. struct drm_i915_fence_reg *reg;
  1216. reg = &dev_priv->fence_regs[obj->fence_reg];
  1217. list_move_tail(&reg->lru_list,
  1218. &dev_priv->mm.fence_list);
  1219. }
  1220. }
  1221. }
  1222. static void
  1223. i915_gem_object_move_off_active(struct drm_i915_gem_object *obj)
  1224. {
  1225. list_del_init(&obj->ring_list);
  1226. obj->last_rendering_seqno = 0;
  1227. obj->last_fenced_seqno = 0;
  1228. }
  1229. static void
  1230. i915_gem_object_move_to_flushing(struct drm_i915_gem_object *obj)
  1231. {
  1232. struct drm_device *dev = obj->base.dev;
  1233. drm_i915_private_t *dev_priv = dev->dev_private;
  1234. BUG_ON(!obj->active);
  1235. list_move_tail(&obj->mm_list, &dev_priv->mm.flushing_list);
  1236. i915_gem_object_move_off_active(obj);
  1237. }
  1238. static void
  1239. i915_gem_object_move_to_inactive(struct drm_i915_gem_object *obj)
  1240. {
  1241. struct drm_device *dev = obj->base.dev;
  1242. struct drm_i915_private *dev_priv = dev->dev_private;
  1243. list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
  1244. BUG_ON(!list_empty(&obj->gpu_write_list));
  1245. BUG_ON(!obj->active);
  1246. obj->ring = NULL;
  1247. i915_gem_object_move_off_active(obj);
  1248. obj->fenced_gpu_access = false;
  1249. obj->active = 0;
  1250. obj->pending_gpu_write = false;
  1251. drm_gem_object_unreference(&obj->base);
  1252. WARN_ON(i915_verify_lists(dev));
  1253. }
  1254. /* Immediately discard the backing storage */
  1255. static void
  1256. i915_gem_object_truncate(struct drm_i915_gem_object *obj)
  1257. {
  1258. struct inode *inode;
  1259. /* Our goal here is to return as much of the memory as
  1260. * is possible back to the system as we are called from OOM.
  1261. * To do this we must instruct the shmfs to drop all of its
  1262. * backing pages, *now*.
  1263. */
  1264. inode = obj->base.filp->f_path.dentry->d_inode;
  1265. shmem_truncate_range(inode, 0, (loff_t)-1);
  1266. if (obj->base.map_list.map)
  1267. drm_gem_free_mmap_offset(&obj->base);
  1268. obj->madv = __I915_MADV_PURGED;
  1269. }
  1270. static inline int
  1271. i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj)
  1272. {
  1273. return obj->madv == I915_MADV_DONTNEED;
  1274. }
  1275. static void
  1276. i915_gem_process_flushing_list(struct intel_ring_buffer *ring,
  1277. uint32_t flush_domains)
  1278. {
  1279. struct drm_i915_gem_object *obj, *next;
  1280. list_for_each_entry_safe(obj, next,
  1281. &ring->gpu_write_list,
  1282. gpu_write_list) {
  1283. if (obj->base.write_domain & flush_domains) {
  1284. uint32_t old_write_domain = obj->base.write_domain;
  1285. obj->base.write_domain = 0;
  1286. list_del_init(&obj->gpu_write_list);
  1287. i915_gem_object_move_to_active(obj, ring,
  1288. i915_gem_next_request_seqno(ring));
  1289. trace_i915_gem_object_change_domain(obj,
  1290. obj->base.read_domains,
  1291. old_write_domain);
  1292. }
  1293. }
  1294. }
  1295. static u32
  1296. i915_gem_get_seqno(struct drm_device *dev)
  1297. {
  1298. drm_i915_private_t *dev_priv = dev->dev_private;
  1299. u32 seqno = dev_priv->next_seqno;
  1300. /* reserve 0 for non-seqno */
  1301. if (++dev_priv->next_seqno == 0)
  1302. dev_priv->next_seqno = 1;
  1303. return seqno;
  1304. }
  1305. u32
  1306. i915_gem_next_request_seqno(struct intel_ring_buffer *ring)
  1307. {
  1308. if (ring->outstanding_lazy_request == 0)
  1309. ring->outstanding_lazy_request = i915_gem_get_seqno(ring->dev);
  1310. return ring->outstanding_lazy_request;
  1311. }
  1312. int
  1313. i915_add_request(struct intel_ring_buffer *ring,
  1314. struct drm_file *file,
  1315. struct drm_i915_gem_request *request)
  1316. {
  1317. drm_i915_private_t *dev_priv = ring->dev->dev_private;
  1318. uint32_t seqno;
  1319. u32 request_ring_position;
  1320. int was_empty;
  1321. int ret;
  1322. BUG_ON(request == NULL);
  1323. seqno = i915_gem_next_request_seqno(ring);
  1324. /* Record the position of the start of the request so that
  1325. * should we detect the updated seqno part-way through the
  1326. * GPU processing the request, we never over-estimate the
  1327. * position of the head.
  1328. */
  1329. request_ring_position = intel_ring_get_tail(ring);
  1330. ret = ring->add_request(ring, &seqno);
  1331. if (ret)
  1332. return ret;
  1333. trace_i915_gem_request_add(ring, seqno);
  1334. request->seqno = seqno;
  1335. request->ring = ring;
  1336. request->tail = request_ring_position;
  1337. request->emitted_jiffies = jiffies;
  1338. was_empty = list_empty(&ring->request_list);
  1339. list_add_tail(&request->list, &ring->request_list);
  1340. if (file) {
  1341. struct drm_i915_file_private *file_priv = file->driver_priv;
  1342. spin_lock(&file_priv->mm.lock);
  1343. request->file_priv = file_priv;
  1344. list_add_tail(&request->client_list,
  1345. &file_priv->mm.request_list);
  1346. spin_unlock(&file_priv->mm.lock);
  1347. }
  1348. ring->outstanding_lazy_request = 0;
  1349. if (!dev_priv->mm.suspended) {
  1350. if (i915_enable_hangcheck) {
  1351. mod_timer(&dev_priv->hangcheck_timer,
  1352. jiffies +
  1353. msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD));
  1354. }
  1355. if (was_empty)
  1356. queue_delayed_work(dev_priv->wq,
  1357. &dev_priv->mm.retire_work, HZ);
  1358. }
  1359. return 0;
  1360. }
  1361. static inline void
  1362. i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
  1363. {
  1364. struct drm_i915_file_private *file_priv = request->file_priv;
  1365. if (!file_priv)
  1366. return;
  1367. spin_lock(&file_priv->mm.lock);
  1368. if (request->file_priv) {
  1369. list_del(&request->client_list);
  1370. request->file_priv = NULL;
  1371. }
  1372. spin_unlock(&file_priv->mm.lock);
  1373. }
  1374. static void i915_gem_reset_ring_lists(struct drm_i915_private *dev_priv,
  1375. struct intel_ring_buffer *ring)
  1376. {
  1377. while (!list_empty(&ring->request_list)) {
  1378. struct drm_i915_gem_request *request;
  1379. request = list_first_entry(&ring->request_list,
  1380. struct drm_i915_gem_request,
  1381. list);
  1382. list_del(&request->list);
  1383. i915_gem_request_remove_from_client(request);
  1384. kfree(request);
  1385. }
  1386. while (!list_empty(&ring->active_list)) {
  1387. struct drm_i915_gem_object *obj;
  1388. obj = list_first_entry(&ring->active_list,
  1389. struct drm_i915_gem_object,
  1390. ring_list);
  1391. obj->base.write_domain = 0;
  1392. list_del_init(&obj->gpu_write_list);
  1393. i915_gem_object_move_to_inactive(obj);
  1394. }
  1395. }
  1396. static void i915_gem_reset_fences(struct drm_device *dev)
  1397. {
  1398. struct drm_i915_private *dev_priv = dev->dev_private;
  1399. int i;
  1400. for (i = 0; i < dev_priv->num_fence_regs; i++) {
  1401. struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i];
  1402. i915_gem_write_fence(dev, i, NULL);
  1403. if (reg->obj)
  1404. i915_gem_object_fence_lost(reg->obj);
  1405. reg->pin_count = 0;
  1406. reg->obj = NULL;
  1407. INIT_LIST_HEAD(&reg->lru_list);
  1408. }
  1409. INIT_LIST_HEAD(&dev_priv->mm.fence_list);
  1410. }
  1411. void i915_gem_reset(struct drm_device *dev)
  1412. {
  1413. struct drm_i915_private *dev_priv = dev->dev_private;
  1414. struct drm_i915_gem_object *obj;
  1415. struct intel_ring_buffer *ring;
  1416. int i;
  1417. for_each_ring(ring, dev_priv, i)
  1418. i915_gem_reset_ring_lists(dev_priv, ring);
  1419. /* Remove anything from the flushing lists. The GPU cache is likely
  1420. * to be lost on reset along with the data, so simply move the
  1421. * lost bo to the inactive list.
  1422. */
  1423. while (!list_empty(&dev_priv->mm.flushing_list)) {
  1424. obj = list_first_entry(&dev_priv->mm.flushing_list,
  1425. struct drm_i915_gem_object,
  1426. mm_list);
  1427. obj->base.write_domain = 0;
  1428. list_del_init(&obj->gpu_write_list);
  1429. i915_gem_object_move_to_inactive(obj);
  1430. }
  1431. /* Move everything out of the GPU domains to ensure we do any
  1432. * necessary invalidation upon reuse.
  1433. */
  1434. list_for_each_entry(obj,
  1435. &dev_priv->mm.inactive_list,
  1436. mm_list)
  1437. {
  1438. obj->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
  1439. }
  1440. /* The fence registers are invalidated so clear them out */
  1441. i915_gem_reset_fences(dev);
  1442. }
  1443. /**
  1444. * This function clears the request list as sequence numbers are passed.
  1445. */
  1446. void
  1447. i915_gem_retire_requests_ring(struct intel_ring_buffer *ring)
  1448. {
  1449. uint32_t seqno;
  1450. int i;
  1451. if (list_empty(&ring->request_list))
  1452. return;
  1453. WARN_ON(i915_verify_lists(ring->dev));
  1454. seqno = ring->get_seqno(ring);
  1455. for (i = 0; i < ARRAY_SIZE(ring->sync_seqno); i++)
  1456. if (seqno >= ring->sync_seqno[i])
  1457. ring->sync_seqno[i] = 0;
  1458. while (!list_empty(&ring->request_list)) {
  1459. struct drm_i915_gem_request *request;
  1460. request = list_first_entry(&ring->request_list,
  1461. struct drm_i915_gem_request,
  1462. list);
  1463. if (!i915_seqno_passed(seqno, request->seqno))
  1464. break;
  1465. trace_i915_gem_request_retire(ring, request->seqno);
  1466. /* We know the GPU must have read the request to have
  1467. * sent us the seqno + interrupt, so use the position
  1468. * of tail of the request to update the last known position
  1469. * of the GPU head.
  1470. */
  1471. ring->last_retired_head = request->tail;
  1472. list_del(&request->list);
  1473. i915_gem_request_remove_from_client(request);
  1474. kfree(request);
  1475. }
  1476. /* Move any buffers on the active list that are no longer referenced
  1477. * by the ringbuffer to the flushing/inactive lists as appropriate.
  1478. */
  1479. while (!list_empty(&ring->active_list)) {
  1480. struct drm_i915_gem_object *obj;
  1481. obj = list_first_entry(&ring->active_list,
  1482. struct drm_i915_gem_object,
  1483. ring_list);
  1484. if (!i915_seqno_passed(seqno, obj->last_rendering_seqno))
  1485. break;
  1486. if (obj->base.write_domain != 0)
  1487. i915_gem_object_move_to_flushing(obj);
  1488. else
  1489. i915_gem_object_move_to_inactive(obj);
  1490. }
  1491. if (unlikely(ring->trace_irq_seqno &&
  1492. i915_seqno_passed(seqno, ring->trace_irq_seqno))) {
  1493. ring->irq_put(ring);
  1494. ring->trace_irq_seqno = 0;
  1495. }
  1496. WARN_ON(i915_verify_lists(ring->dev));
  1497. }
  1498. void
  1499. i915_gem_retire_requests(struct drm_device *dev)
  1500. {
  1501. drm_i915_private_t *dev_priv = dev->dev_private;
  1502. struct intel_ring_buffer *ring;
  1503. int i;
  1504. for_each_ring(ring, dev_priv, i)
  1505. i915_gem_retire_requests_ring(ring);
  1506. }
  1507. static void
  1508. i915_gem_retire_work_handler(struct work_struct *work)
  1509. {
  1510. drm_i915_private_t *dev_priv;
  1511. struct drm_device *dev;
  1512. struct intel_ring_buffer *ring;
  1513. bool idle;
  1514. int i;
  1515. dev_priv = container_of(work, drm_i915_private_t,
  1516. mm.retire_work.work);
  1517. dev = dev_priv->dev;
  1518. /* Come back later if the device is busy... */
  1519. if (!mutex_trylock(&dev->struct_mutex)) {
  1520. queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
  1521. return;
  1522. }
  1523. i915_gem_retire_requests(dev);
  1524. /* Send a periodic flush down the ring so we don't hold onto GEM
  1525. * objects indefinitely.
  1526. */
  1527. idle = true;
  1528. for_each_ring(ring, dev_priv, i) {
  1529. if (!list_empty(&ring->gpu_write_list)) {
  1530. struct drm_i915_gem_request *request;
  1531. int ret;
  1532. ret = i915_gem_flush_ring(ring,
  1533. 0, I915_GEM_GPU_DOMAINS);
  1534. request = kzalloc(sizeof(*request), GFP_KERNEL);
  1535. if (ret || request == NULL ||
  1536. i915_add_request(ring, NULL, request))
  1537. kfree(request);
  1538. }
  1539. idle &= list_empty(&ring->request_list);
  1540. }
  1541. if (!dev_priv->mm.suspended && !idle)
  1542. queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
  1543. mutex_unlock(&dev->struct_mutex);
  1544. }
  1545. static int
  1546. i915_gem_check_wedge(struct drm_i915_private *dev_priv)
  1547. {
  1548. BUG_ON(!mutex_is_locked(&dev_priv->dev->struct_mutex));
  1549. if (atomic_read(&dev_priv->mm.wedged)) {
  1550. struct completion *x = &dev_priv->error_completion;
  1551. bool recovery_complete;
  1552. unsigned long flags;
  1553. /* Give the error handler a chance to run. */
  1554. spin_lock_irqsave(&x->wait.lock, flags);
  1555. recovery_complete = x->done > 0;
  1556. spin_unlock_irqrestore(&x->wait.lock, flags);
  1557. return recovery_complete ? -EIO : -EAGAIN;
  1558. }
  1559. return 0;
  1560. }
  1561. /*
  1562. * Compare seqno against outstanding lazy request. Emit a request if they are
  1563. * equal.
  1564. */
  1565. static int
  1566. i915_gem_check_olr(struct intel_ring_buffer *ring, u32 seqno)
  1567. {
  1568. int ret = 0;
  1569. BUG_ON(!mutex_is_locked(&ring->dev->struct_mutex));
  1570. if (seqno == ring->outstanding_lazy_request) {
  1571. struct drm_i915_gem_request *request;
  1572. request = kzalloc(sizeof(*request), GFP_KERNEL);
  1573. if (request == NULL)
  1574. return -ENOMEM;
  1575. ret = i915_add_request(ring, NULL, request);
  1576. if (ret) {
  1577. kfree(request);
  1578. return ret;
  1579. }
  1580. BUG_ON(seqno != request->seqno);
  1581. }
  1582. return ret;
  1583. }
  1584. static int __wait_seqno(struct intel_ring_buffer *ring, u32 seqno,
  1585. bool interruptible)
  1586. {
  1587. drm_i915_private_t *dev_priv = ring->dev->dev_private;
  1588. int ret = 0;
  1589. if (i915_seqno_passed(ring->get_seqno(ring), seqno))
  1590. return 0;
  1591. trace_i915_gem_request_wait_begin(ring, seqno);
  1592. if (WARN_ON(!ring->irq_get(ring)))
  1593. return -ENODEV;
  1594. #define EXIT_COND \
  1595. (i915_seqno_passed(ring->get_seqno(ring), seqno) || \
  1596. atomic_read(&dev_priv->mm.wedged))
  1597. if (interruptible)
  1598. ret = wait_event_interruptible(ring->irq_queue,
  1599. EXIT_COND);
  1600. else
  1601. wait_event(ring->irq_queue, EXIT_COND);
  1602. ring->irq_put(ring);
  1603. trace_i915_gem_request_wait_end(ring, seqno);
  1604. #undef EXIT_COND
  1605. return ret;
  1606. }
  1607. /**
  1608. * Waits for a sequence number to be signaled, and cleans up the
  1609. * request and object lists appropriately for that event.
  1610. */
  1611. int
  1612. i915_wait_request(struct intel_ring_buffer *ring,
  1613. uint32_t seqno)
  1614. {
  1615. drm_i915_private_t *dev_priv = ring->dev->dev_private;
  1616. int ret = 0;
  1617. BUG_ON(seqno == 0);
  1618. ret = i915_gem_check_wedge(dev_priv);
  1619. if (ret)
  1620. return ret;
  1621. ret = i915_gem_check_olr(ring, seqno);
  1622. if (ret)
  1623. return ret;
  1624. ret = __wait_seqno(ring, seqno, dev_priv->mm.interruptible);
  1625. if (atomic_read(&dev_priv->mm.wedged))
  1626. ret = -EAGAIN;
  1627. return ret;
  1628. }
  1629. /**
  1630. * Ensures that all rendering to the object has completed and the object is
  1631. * safe to unbind from the GTT or access from the CPU.
  1632. */
  1633. int
  1634. i915_gem_object_wait_rendering(struct drm_i915_gem_object *obj)
  1635. {
  1636. int ret;
  1637. /* This function only exists to support waiting for existing rendering,
  1638. * not for emitting required flushes.
  1639. */
  1640. BUG_ON((obj->base.write_domain & I915_GEM_GPU_DOMAINS) != 0);
  1641. /* If there is rendering queued on the buffer being evicted, wait for
  1642. * it.
  1643. */
  1644. if (obj->active) {
  1645. ret = i915_wait_request(obj->ring, obj->last_rendering_seqno);
  1646. if (ret)
  1647. return ret;
  1648. i915_gem_retire_requests_ring(obj->ring);
  1649. }
  1650. return 0;
  1651. }
  1652. /**
  1653. * i915_gem_object_sync - sync an object to a ring.
  1654. *
  1655. * @obj: object which may be in use on another ring.
  1656. * @to: ring we wish to use the object on. May be NULL.
  1657. *
  1658. * This code is meant to abstract object synchronization with the GPU.
  1659. * Calling with NULL implies synchronizing the object with the CPU
  1660. * rather than a particular GPU ring.
  1661. *
  1662. * Returns 0 if successful, else propagates up the lower layer error.
  1663. */
  1664. int
  1665. i915_gem_object_sync(struct drm_i915_gem_object *obj,
  1666. struct intel_ring_buffer *to)
  1667. {
  1668. struct intel_ring_buffer *from = obj->ring;
  1669. u32 seqno;
  1670. int ret, idx;
  1671. if (from == NULL || to == from)
  1672. return 0;
  1673. if (to == NULL || !i915_semaphore_is_enabled(obj->base.dev))
  1674. return i915_gem_object_wait_rendering(obj);
  1675. idx = intel_ring_sync_index(from, to);
  1676. seqno = obj->last_rendering_seqno;
  1677. if (seqno <= from->sync_seqno[idx])
  1678. return 0;
  1679. ret = i915_gem_check_olr(obj->ring, seqno);
  1680. if (ret)
  1681. return ret;
  1682. ret = to->sync_to(to, from, seqno);
  1683. if (!ret)
  1684. from->sync_seqno[idx] = seqno;
  1685. return ret;
  1686. }
  1687. static void i915_gem_object_finish_gtt(struct drm_i915_gem_object *obj)
  1688. {
  1689. u32 old_write_domain, old_read_domains;
  1690. /* Act a barrier for all accesses through the GTT */
  1691. mb();
  1692. /* Force a pagefault for domain tracking on next user access */
  1693. i915_gem_release_mmap(obj);
  1694. if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
  1695. return;
  1696. old_read_domains = obj->base.read_domains;
  1697. old_write_domain = obj->base.write_domain;
  1698. obj->base.read_domains &= ~I915_GEM_DOMAIN_GTT;
  1699. obj->base.write_domain &= ~I915_GEM_DOMAIN_GTT;
  1700. trace_i915_gem_object_change_domain(obj,
  1701. old_read_domains,
  1702. old_write_domain);
  1703. }
  1704. /**
  1705. * Unbinds an object from the GTT aperture.
  1706. */
  1707. int
  1708. i915_gem_object_unbind(struct drm_i915_gem_object *obj)
  1709. {
  1710. drm_i915_private_t *dev_priv = obj->base.dev->dev_private;
  1711. int ret = 0;
  1712. if (obj->gtt_space == NULL)
  1713. return 0;
  1714. if (obj->pin_count)
  1715. return -EBUSY;
  1716. ret = i915_gem_object_finish_gpu(obj);
  1717. if (ret)
  1718. return ret;
  1719. /* Continue on if we fail due to EIO, the GPU is hung so we
  1720. * should be safe and we need to cleanup or else we might
  1721. * cause memory corruption through use-after-free.
  1722. */
  1723. i915_gem_object_finish_gtt(obj);
  1724. /* Move the object to the CPU domain to ensure that
  1725. * any possible CPU writes while it's not in the GTT
  1726. * are flushed when we go to remap it.
  1727. */
  1728. if (ret == 0)
  1729. ret = i915_gem_object_set_to_cpu_domain(obj, 1);
  1730. if (ret == -ERESTARTSYS)
  1731. return ret;
  1732. if (ret) {
  1733. /* In the event of a disaster, abandon all caches and
  1734. * hope for the best.
  1735. */
  1736. i915_gem_clflush_object(obj);
  1737. obj->base.read_domains = obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  1738. }
  1739. /* release the fence reg _after_ flushing */
  1740. ret = i915_gem_object_put_fence(obj);
  1741. if (ret)
  1742. return ret;
  1743. trace_i915_gem_object_unbind(obj);
  1744. if (obj->has_global_gtt_mapping)
  1745. i915_gem_gtt_unbind_object(obj);
  1746. if (obj->has_aliasing_ppgtt_mapping) {
  1747. i915_ppgtt_unbind_object(dev_priv->mm.aliasing_ppgtt, obj);
  1748. obj->has_aliasing_ppgtt_mapping = 0;
  1749. }
  1750. i915_gem_gtt_finish_object(obj);
  1751. i915_gem_object_put_pages_gtt(obj);
  1752. list_del_init(&obj->gtt_list);
  1753. list_del_init(&obj->mm_list);
  1754. /* Avoid an unnecessary call to unbind on rebind. */
  1755. obj->map_and_fenceable = true;
  1756. drm_mm_put_block(obj->gtt_space);
  1757. obj->gtt_space = NULL;
  1758. obj->gtt_offset = 0;
  1759. if (i915_gem_object_is_purgeable(obj))
  1760. i915_gem_object_truncate(obj);
  1761. return ret;
  1762. }
  1763. int
  1764. i915_gem_flush_ring(struct intel_ring_buffer *ring,
  1765. uint32_t invalidate_domains,
  1766. uint32_t flush_domains)
  1767. {
  1768. int ret;
  1769. if (((invalidate_domains | flush_domains) & I915_GEM_GPU_DOMAINS) == 0)
  1770. return 0;
  1771. trace_i915_gem_ring_flush(ring, invalidate_domains, flush_domains);
  1772. ret = ring->flush(ring, invalidate_domains, flush_domains);
  1773. if (ret)
  1774. return ret;
  1775. if (flush_domains & I915_GEM_GPU_DOMAINS)
  1776. i915_gem_process_flushing_list(ring, flush_domains);
  1777. return 0;
  1778. }
  1779. static int i915_ring_idle(struct intel_ring_buffer *ring)
  1780. {
  1781. int ret;
  1782. if (list_empty(&ring->gpu_write_list) && list_empty(&ring->active_list))
  1783. return 0;
  1784. if (!list_empty(&ring->gpu_write_list)) {
  1785. ret = i915_gem_flush_ring(ring,
  1786. I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
  1787. if (ret)
  1788. return ret;
  1789. }
  1790. return i915_wait_request(ring, i915_gem_next_request_seqno(ring));
  1791. }
  1792. int i915_gpu_idle(struct drm_device *dev)
  1793. {
  1794. drm_i915_private_t *dev_priv = dev->dev_private;
  1795. struct intel_ring_buffer *ring;
  1796. int ret, i;
  1797. /* Flush everything onto the inactive list. */
  1798. for_each_ring(ring, dev_priv, i) {
  1799. ret = i915_ring_idle(ring);
  1800. if (ret)
  1801. return ret;
  1802. /* Is the device fubar? */
  1803. if (WARN_ON(!list_empty(&ring->gpu_write_list)))
  1804. return -EBUSY;
  1805. }
  1806. return 0;
  1807. }
  1808. static void sandybridge_write_fence_reg(struct drm_device *dev, int reg,
  1809. struct drm_i915_gem_object *obj)
  1810. {
  1811. drm_i915_private_t *dev_priv = dev->dev_private;
  1812. uint64_t val;
  1813. if (obj) {
  1814. u32 size = obj->gtt_space->size;
  1815. val = (uint64_t)((obj->gtt_offset + size - 4096) &
  1816. 0xfffff000) << 32;
  1817. val |= obj->gtt_offset & 0xfffff000;
  1818. val |= (uint64_t)((obj->stride / 128) - 1) <<
  1819. SANDYBRIDGE_FENCE_PITCH_SHIFT;
  1820. if (obj->tiling_mode == I915_TILING_Y)
  1821. val |= 1 << I965_FENCE_TILING_Y_SHIFT;
  1822. val |= I965_FENCE_REG_VALID;
  1823. } else
  1824. val = 0;
  1825. I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + reg * 8, val);
  1826. POSTING_READ(FENCE_REG_SANDYBRIDGE_0 + reg * 8);
  1827. }
  1828. static void i965_write_fence_reg(struct drm_device *dev, int reg,
  1829. struct drm_i915_gem_object *obj)
  1830. {
  1831. drm_i915_private_t *dev_priv = dev->dev_private;
  1832. uint64_t val;
  1833. if (obj) {
  1834. u32 size = obj->gtt_space->size;
  1835. val = (uint64_t)((obj->gtt_offset + size - 4096) &
  1836. 0xfffff000) << 32;
  1837. val |= obj->gtt_offset & 0xfffff000;
  1838. val |= ((obj->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
  1839. if (obj->tiling_mode == I915_TILING_Y)
  1840. val |= 1 << I965_FENCE_TILING_Y_SHIFT;
  1841. val |= I965_FENCE_REG_VALID;
  1842. } else
  1843. val = 0;
  1844. I915_WRITE64(FENCE_REG_965_0 + reg * 8, val);
  1845. POSTING_READ(FENCE_REG_965_0 + reg * 8);
  1846. }
  1847. static void i915_write_fence_reg(struct drm_device *dev, int reg,
  1848. struct drm_i915_gem_object *obj)
  1849. {
  1850. drm_i915_private_t *dev_priv = dev->dev_private;
  1851. u32 val;
  1852. if (obj) {
  1853. u32 size = obj->gtt_space->size;
  1854. int pitch_val;
  1855. int tile_width;
  1856. WARN((obj->gtt_offset & ~I915_FENCE_START_MASK) ||
  1857. (size & -size) != size ||
  1858. (obj->gtt_offset & (size - 1)),
  1859. "object 0x%08x [fenceable? %d] not 1M or pot-size (0x%08x) aligned\n",
  1860. obj->gtt_offset, obj->map_and_fenceable, size);
  1861. if (obj->tiling_mode == I915_TILING_Y && HAS_128_BYTE_Y_TILING(dev))
  1862. tile_width = 128;
  1863. else
  1864. tile_width = 512;
  1865. /* Note: pitch better be a power of two tile widths */
  1866. pitch_val = obj->stride / tile_width;
  1867. pitch_val = ffs(pitch_val) - 1;
  1868. val = obj->gtt_offset;
  1869. if (obj->tiling_mode == I915_TILING_Y)
  1870. val |= 1 << I830_FENCE_TILING_Y_SHIFT;
  1871. val |= I915_FENCE_SIZE_BITS(size);
  1872. val |= pitch_val << I830_FENCE_PITCH_SHIFT;
  1873. val |= I830_FENCE_REG_VALID;
  1874. } else
  1875. val = 0;
  1876. if (reg < 8)
  1877. reg = FENCE_REG_830_0 + reg * 4;
  1878. else
  1879. reg = FENCE_REG_945_8 + (reg - 8) * 4;
  1880. I915_WRITE(reg, val);
  1881. POSTING_READ(reg);
  1882. }
  1883. static void i830_write_fence_reg(struct drm_device *dev, int reg,
  1884. struct drm_i915_gem_object *obj)
  1885. {
  1886. drm_i915_private_t *dev_priv = dev->dev_private;
  1887. uint32_t val;
  1888. if (obj) {
  1889. u32 size = obj->gtt_space->size;
  1890. uint32_t pitch_val;
  1891. WARN((obj->gtt_offset & ~I830_FENCE_START_MASK) ||
  1892. (size & -size) != size ||
  1893. (obj->gtt_offset & (size - 1)),
  1894. "object 0x%08x not 512K or pot-size 0x%08x aligned\n",
  1895. obj->gtt_offset, size);
  1896. pitch_val = obj->stride / 128;
  1897. pitch_val = ffs(pitch_val) - 1;
  1898. val = obj->gtt_offset;
  1899. if (obj->tiling_mode == I915_TILING_Y)
  1900. val |= 1 << I830_FENCE_TILING_Y_SHIFT;
  1901. val |= I830_FENCE_SIZE_BITS(size);
  1902. val |= pitch_val << I830_FENCE_PITCH_SHIFT;
  1903. val |= I830_FENCE_REG_VALID;
  1904. } else
  1905. val = 0;
  1906. I915_WRITE(FENCE_REG_830_0 + reg * 4, val);
  1907. POSTING_READ(FENCE_REG_830_0 + reg * 4);
  1908. }
  1909. static void i915_gem_write_fence(struct drm_device *dev, int reg,
  1910. struct drm_i915_gem_object *obj)
  1911. {
  1912. switch (INTEL_INFO(dev)->gen) {
  1913. case 7:
  1914. case 6: sandybridge_write_fence_reg(dev, reg, obj); break;
  1915. case 5:
  1916. case 4: i965_write_fence_reg(dev, reg, obj); break;
  1917. case 3: i915_write_fence_reg(dev, reg, obj); break;
  1918. case 2: i830_write_fence_reg(dev, reg, obj); break;
  1919. default: break;
  1920. }
  1921. }
  1922. static inline int fence_number(struct drm_i915_private *dev_priv,
  1923. struct drm_i915_fence_reg *fence)
  1924. {
  1925. return fence - dev_priv->fence_regs;
  1926. }
  1927. static void i915_gem_object_update_fence(struct drm_i915_gem_object *obj,
  1928. struct drm_i915_fence_reg *fence,
  1929. bool enable)
  1930. {
  1931. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  1932. int reg = fence_number(dev_priv, fence);
  1933. i915_gem_write_fence(obj->base.dev, reg, enable ? obj : NULL);
  1934. if (enable) {
  1935. obj->fence_reg = reg;
  1936. fence->obj = obj;
  1937. list_move_tail(&fence->lru_list, &dev_priv->mm.fence_list);
  1938. } else {
  1939. obj->fence_reg = I915_FENCE_REG_NONE;
  1940. fence->obj = NULL;
  1941. list_del_init(&fence->lru_list);
  1942. }
  1943. }
  1944. static int
  1945. i915_gem_object_flush_fence(struct drm_i915_gem_object *obj)
  1946. {
  1947. int ret;
  1948. if (obj->fenced_gpu_access) {
  1949. if (obj->base.write_domain & I915_GEM_GPU_DOMAINS) {
  1950. ret = i915_gem_flush_ring(obj->ring,
  1951. 0, obj->base.write_domain);
  1952. if (ret)
  1953. return ret;
  1954. }
  1955. obj->fenced_gpu_access = false;
  1956. }
  1957. if (obj->last_fenced_seqno) {
  1958. ret = i915_wait_request(obj->ring, obj->last_fenced_seqno);
  1959. if (ret)
  1960. return ret;
  1961. obj->last_fenced_seqno = 0;
  1962. }
  1963. /* Ensure that all CPU reads are completed before installing a fence
  1964. * and all writes before removing the fence.
  1965. */
  1966. if (obj->base.read_domains & I915_GEM_DOMAIN_GTT)
  1967. mb();
  1968. return 0;
  1969. }
  1970. int
  1971. i915_gem_object_put_fence(struct drm_i915_gem_object *obj)
  1972. {
  1973. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  1974. int ret;
  1975. ret = i915_gem_object_flush_fence(obj);
  1976. if (ret)
  1977. return ret;
  1978. if (obj->fence_reg == I915_FENCE_REG_NONE)
  1979. return 0;
  1980. i915_gem_object_update_fence(obj,
  1981. &dev_priv->fence_regs[obj->fence_reg],
  1982. false);
  1983. i915_gem_object_fence_lost(obj);
  1984. return 0;
  1985. }
  1986. static struct drm_i915_fence_reg *
  1987. i915_find_fence_reg(struct drm_device *dev)
  1988. {
  1989. struct drm_i915_private *dev_priv = dev->dev_private;
  1990. struct drm_i915_fence_reg *reg, *avail;
  1991. int i;
  1992. /* First try to find a free reg */
  1993. avail = NULL;
  1994. for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
  1995. reg = &dev_priv->fence_regs[i];
  1996. if (!reg->obj)
  1997. return reg;
  1998. if (!reg->pin_count)
  1999. avail = reg;
  2000. }
  2001. if (avail == NULL)
  2002. return NULL;
  2003. /* None available, try to steal one or wait for a user to finish */
  2004. list_for_each_entry(reg, &dev_priv->mm.fence_list, lru_list) {
  2005. if (reg->pin_count)
  2006. continue;
  2007. return reg;
  2008. }
  2009. return NULL;
  2010. }
  2011. /**
  2012. * i915_gem_object_get_fence - set up fencing for an object
  2013. * @obj: object to map through a fence reg
  2014. *
  2015. * When mapping objects through the GTT, userspace wants to be able to write
  2016. * to them without having to worry about swizzling if the object is tiled.
  2017. * This function walks the fence regs looking for a free one for @obj,
  2018. * stealing one if it can't find any.
  2019. *
  2020. * It then sets up the reg based on the object's properties: address, pitch
  2021. * and tiling format.
  2022. *
  2023. * For an untiled surface, this removes any existing fence.
  2024. */
  2025. int
  2026. i915_gem_object_get_fence(struct drm_i915_gem_object *obj)
  2027. {
  2028. struct drm_device *dev = obj->base.dev;
  2029. struct drm_i915_private *dev_priv = dev->dev_private;
  2030. bool enable = obj->tiling_mode != I915_TILING_NONE;
  2031. struct drm_i915_fence_reg *reg;
  2032. int ret;
  2033. /* Have we updated the tiling parameters upon the object and so
  2034. * will need to serialise the write to the associated fence register?
  2035. */
  2036. if (obj->fence_dirty) {
  2037. ret = i915_gem_object_flush_fence(obj);
  2038. if (ret)
  2039. return ret;
  2040. }
  2041. /* Just update our place in the LRU if our fence is getting reused. */
  2042. if (obj->fence_reg != I915_FENCE_REG_NONE) {
  2043. reg = &dev_priv->fence_regs[obj->fence_reg];
  2044. if (!obj->fence_dirty) {
  2045. list_move_tail(&reg->lru_list,
  2046. &dev_priv->mm.fence_list);
  2047. return 0;
  2048. }
  2049. } else if (enable) {
  2050. reg = i915_find_fence_reg(dev);
  2051. if (reg == NULL)
  2052. return -EDEADLK;
  2053. if (reg->obj) {
  2054. struct drm_i915_gem_object *old = reg->obj;
  2055. ret = i915_gem_object_flush_fence(old);
  2056. if (ret)
  2057. return ret;
  2058. i915_gem_object_fence_lost(old);
  2059. }
  2060. } else
  2061. return 0;
  2062. i915_gem_object_update_fence(obj, reg, enable);
  2063. obj->fence_dirty = false;
  2064. return 0;
  2065. }
  2066. /**
  2067. * Finds free space in the GTT aperture and binds the object there.
  2068. */
  2069. static int
  2070. i915_gem_object_bind_to_gtt(struct drm_i915_gem_object *obj,
  2071. unsigned alignment,
  2072. bool map_and_fenceable)
  2073. {
  2074. struct drm_device *dev = obj->base.dev;
  2075. drm_i915_private_t *dev_priv = dev->dev_private;
  2076. struct drm_mm_node *free_space;
  2077. gfp_t gfpmask = __GFP_NORETRY | __GFP_NOWARN;
  2078. u32 size, fence_size, fence_alignment, unfenced_alignment;
  2079. bool mappable, fenceable;
  2080. int ret;
  2081. if (obj->madv != I915_MADV_WILLNEED) {
  2082. DRM_ERROR("Attempting to bind a purgeable object\n");
  2083. return -EINVAL;
  2084. }
  2085. fence_size = i915_gem_get_gtt_size(dev,
  2086. obj->base.size,
  2087. obj->tiling_mode);
  2088. fence_alignment = i915_gem_get_gtt_alignment(dev,
  2089. obj->base.size,
  2090. obj->tiling_mode);
  2091. unfenced_alignment =
  2092. i915_gem_get_unfenced_gtt_alignment(dev,
  2093. obj->base.size,
  2094. obj->tiling_mode);
  2095. if (alignment == 0)
  2096. alignment = map_and_fenceable ? fence_alignment :
  2097. unfenced_alignment;
  2098. if (map_and_fenceable && alignment & (fence_alignment - 1)) {
  2099. DRM_ERROR("Invalid object alignment requested %u\n", alignment);
  2100. return -EINVAL;
  2101. }
  2102. size = map_and_fenceable ? fence_size : obj->base.size;
  2103. /* If the object is bigger than the entire aperture, reject it early
  2104. * before evicting everything in a vain attempt to find space.
  2105. */
  2106. if (obj->base.size >
  2107. (map_and_fenceable ? dev_priv->mm.gtt_mappable_end : dev_priv->mm.gtt_total)) {
  2108. DRM_ERROR("Attempting to bind an object larger than the aperture\n");
  2109. return -E2BIG;
  2110. }
  2111. search_free:
  2112. if (map_and_fenceable)
  2113. free_space =
  2114. drm_mm_search_free_in_range(&dev_priv->mm.gtt_space,
  2115. size, alignment, 0,
  2116. dev_priv->mm.gtt_mappable_end,
  2117. 0);
  2118. else
  2119. free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
  2120. size, alignment, 0);
  2121. if (free_space != NULL) {
  2122. if (map_and_fenceable)
  2123. obj->gtt_space =
  2124. drm_mm_get_block_range_generic(free_space,
  2125. size, alignment, 0,
  2126. dev_priv->mm.gtt_mappable_end,
  2127. 0);
  2128. else
  2129. obj->gtt_space =
  2130. drm_mm_get_block(free_space, size, alignment);
  2131. }
  2132. if (obj->gtt_space == NULL) {
  2133. /* If the gtt is empty and we're still having trouble
  2134. * fitting our object in, we're out of memory.
  2135. */
  2136. ret = i915_gem_evict_something(dev, size, alignment,
  2137. map_and_fenceable);
  2138. if (ret)
  2139. return ret;
  2140. goto search_free;
  2141. }
  2142. ret = i915_gem_object_get_pages_gtt(obj, gfpmask);
  2143. if (ret) {
  2144. drm_mm_put_block(obj->gtt_space);
  2145. obj->gtt_space = NULL;
  2146. if (ret == -ENOMEM) {
  2147. /* first try to reclaim some memory by clearing the GTT */
  2148. ret = i915_gem_evict_everything(dev, false);
  2149. if (ret) {
  2150. /* now try to shrink everyone else */
  2151. if (gfpmask) {
  2152. gfpmask = 0;
  2153. goto search_free;
  2154. }
  2155. return -ENOMEM;
  2156. }
  2157. goto search_free;
  2158. }
  2159. return ret;
  2160. }
  2161. ret = i915_gem_gtt_prepare_object(obj);
  2162. if (ret) {
  2163. i915_gem_object_put_pages_gtt(obj);
  2164. drm_mm_put_block(obj->gtt_space);
  2165. obj->gtt_space = NULL;
  2166. if (i915_gem_evict_everything(dev, false))
  2167. return ret;
  2168. goto search_free;
  2169. }
  2170. if (!dev_priv->mm.aliasing_ppgtt)
  2171. i915_gem_gtt_bind_object(obj, obj->cache_level);
  2172. list_add_tail(&obj->gtt_list, &dev_priv->mm.gtt_list);
  2173. list_add_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
  2174. /* Assert that the object is not currently in any GPU domain. As it
  2175. * wasn't in the GTT, there shouldn't be any way it could have been in
  2176. * a GPU cache
  2177. */
  2178. BUG_ON(obj->base.read_domains & I915_GEM_GPU_DOMAINS);
  2179. BUG_ON(obj->base.write_domain & I915_GEM_GPU_DOMAINS);
  2180. obj->gtt_offset = obj->gtt_space->start;
  2181. fenceable =
  2182. obj->gtt_space->size == fence_size &&
  2183. (obj->gtt_space->start & (fence_alignment - 1)) == 0;
  2184. mappable =
  2185. obj->gtt_offset + obj->base.size <= dev_priv->mm.gtt_mappable_end;
  2186. obj->map_and_fenceable = mappable && fenceable;
  2187. trace_i915_gem_object_bind(obj, map_and_fenceable);
  2188. return 0;
  2189. }
  2190. void
  2191. i915_gem_clflush_object(struct drm_i915_gem_object *obj)
  2192. {
  2193. /* If we don't have a page list set up, then we're not pinned
  2194. * to GPU, and we can ignore the cache flush because it'll happen
  2195. * again at bind time.
  2196. */
  2197. if (obj->pages == NULL)
  2198. return;
  2199. /* If the GPU is snooping the contents of the CPU cache,
  2200. * we do not need to manually clear the CPU cache lines. However,
  2201. * the caches are only snooped when the render cache is
  2202. * flushed/invalidated. As we always have to emit invalidations
  2203. * and flushes when moving into and out of the RENDER domain, correct
  2204. * snooping behaviour occurs naturally as the result of our domain
  2205. * tracking.
  2206. */
  2207. if (obj->cache_level != I915_CACHE_NONE)
  2208. return;
  2209. trace_i915_gem_object_clflush(obj);
  2210. drm_clflush_pages(obj->pages, obj->base.size / PAGE_SIZE);
  2211. }
  2212. /** Flushes any GPU write domain for the object if it's dirty. */
  2213. static int
  2214. i915_gem_object_flush_gpu_write_domain(struct drm_i915_gem_object *obj)
  2215. {
  2216. if ((obj->base.write_domain & I915_GEM_GPU_DOMAINS) == 0)
  2217. return 0;
  2218. /* Queue the GPU write cache flushing we need. */
  2219. return i915_gem_flush_ring(obj->ring, 0, obj->base.write_domain);
  2220. }
  2221. /** Flushes the GTT write domain for the object if it's dirty. */
  2222. static void
  2223. i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj)
  2224. {
  2225. uint32_t old_write_domain;
  2226. if (obj->base.write_domain != I915_GEM_DOMAIN_GTT)
  2227. return;
  2228. /* No actual flushing is required for the GTT write domain. Writes
  2229. * to it immediately go to main memory as far as we know, so there's
  2230. * no chipset flush. It also doesn't land in render cache.
  2231. *
  2232. * However, we do have to enforce the order so that all writes through
  2233. * the GTT land before any writes to the device, such as updates to
  2234. * the GATT itself.
  2235. */
  2236. wmb();
  2237. old_write_domain = obj->base.write_domain;
  2238. obj->base.write_domain = 0;
  2239. trace_i915_gem_object_change_domain(obj,
  2240. obj->base.read_domains,
  2241. old_write_domain);
  2242. }
  2243. /** Flushes the CPU write domain for the object if it's dirty. */
  2244. static void
  2245. i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj)
  2246. {
  2247. uint32_t old_write_domain;
  2248. if (obj->base.write_domain != I915_GEM_DOMAIN_CPU)
  2249. return;
  2250. i915_gem_clflush_object(obj);
  2251. intel_gtt_chipset_flush();
  2252. old_write_domain = obj->base.write_domain;
  2253. obj->base.write_domain = 0;
  2254. trace_i915_gem_object_change_domain(obj,
  2255. obj->base.read_domains,
  2256. old_write_domain);
  2257. }
  2258. /**
  2259. * Moves a single object to the GTT read, and possibly write domain.
  2260. *
  2261. * This function returns when the move is complete, including waiting on
  2262. * flushes to occur.
  2263. */
  2264. int
  2265. i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
  2266. {
  2267. drm_i915_private_t *dev_priv = obj->base.dev->dev_private;
  2268. uint32_t old_write_domain, old_read_domains;
  2269. int ret;
  2270. /* Not valid to be called on unbound objects. */
  2271. if (obj->gtt_space == NULL)
  2272. return -EINVAL;
  2273. if (obj->base.write_domain == I915_GEM_DOMAIN_GTT)
  2274. return 0;
  2275. ret = i915_gem_object_flush_gpu_write_domain(obj);
  2276. if (ret)
  2277. return ret;
  2278. if (obj->pending_gpu_write || write) {
  2279. ret = i915_gem_object_wait_rendering(obj);
  2280. if (ret)
  2281. return ret;
  2282. }
  2283. i915_gem_object_flush_cpu_write_domain(obj);
  2284. old_write_domain = obj->base.write_domain;
  2285. old_read_domains = obj->base.read_domains;
  2286. /* It should now be out of any other write domains, and we can update
  2287. * the domain values for our changes.
  2288. */
  2289. BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
  2290. obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
  2291. if (write) {
  2292. obj->base.read_domains = I915_GEM_DOMAIN_GTT;
  2293. obj->base.write_domain = I915_GEM_DOMAIN_GTT;
  2294. obj->dirty = 1;
  2295. }
  2296. trace_i915_gem_object_change_domain(obj,
  2297. old_read_domains,
  2298. old_write_domain);
  2299. /* And bump the LRU for this access */
  2300. if (i915_gem_object_is_inactive(obj))
  2301. list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
  2302. return 0;
  2303. }
  2304. int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
  2305. enum i915_cache_level cache_level)
  2306. {
  2307. struct drm_device *dev = obj->base.dev;
  2308. drm_i915_private_t *dev_priv = dev->dev_private;
  2309. int ret;
  2310. if (obj->cache_level == cache_level)
  2311. return 0;
  2312. if (obj->pin_count) {
  2313. DRM_DEBUG("can not change the cache level of pinned objects\n");
  2314. return -EBUSY;
  2315. }
  2316. if (obj->gtt_space) {
  2317. ret = i915_gem_object_finish_gpu(obj);
  2318. if (ret)
  2319. return ret;
  2320. i915_gem_object_finish_gtt(obj);
  2321. /* Before SandyBridge, you could not use tiling or fence
  2322. * registers with snooped memory, so relinquish any fences
  2323. * currently pointing to our region in the aperture.
  2324. */
  2325. if (INTEL_INFO(obj->base.dev)->gen < 6) {
  2326. ret = i915_gem_object_put_fence(obj);
  2327. if (ret)
  2328. return ret;
  2329. }
  2330. if (obj->has_global_gtt_mapping)
  2331. i915_gem_gtt_bind_object(obj, cache_level);
  2332. if (obj->has_aliasing_ppgtt_mapping)
  2333. i915_ppgtt_bind_object(dev_priv->mm.aliasing_ppgtt,
  2334. obj, cache_level);
  2335. }
  2336. if (cache_level == I915_CACHE_NONE) {
  2337. u32 old_read_domains, old_write_domain;
  2338. /* If we're coming from LLC cached, then we haven't
  2339. * actually been tracking whether the data is in the
  2340. * CPU cache or not, since we only allow one bit set
  2341. * in obj->write_domain and have been skipping the clflushes.
  2342. * Just set it to the CPU cache for now.
  2343. */
  2344. WARN_ON(obj->base.write_domain & ~I915_GEM_DOMAIN_CPU);
  2345. WARN_ON(obj->base.read_domains & ~I915_GEM_DOMAIN_CPU);
  2346. old_read_domains = obj->base.read_domains;
  2347. old_write_domain = obj->base.write_domain;
  2348. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  2349. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  2350. trace_i915_gem_object_change_domain(obj,
  2351. old_read_domains,
  2352. old_write_domain);
  2353. }
  2354. obj->cache_level = cache_level;
  2355. return 0;
  2356. }
  2357. /*
  2358. * Prepare buffer for display plane (scanout, cursors, etc).
  2359. * Can be called from an uninterruptible phase (modesetting) and allows
  2360. * any flushes to be pipelined (for pageflips).
  2361. */
  2362. int
  2363. i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
  2364. u32 alignment,
  2365. struct intel_ring_buffer *pipelined)
  2366. {
  2367. u32 old_read_domains, old_write_domain;
  2368. int ret;
  2369. ret = i915_gem_object_flush_gpu_write_domain(obj);
  2370. if (ret)
  2371. return ret;
  2372. if (pipelined != obj->ring) {
  2373. ret = i915_gem_object_sync(obj, pipelined);
  2374. if (ret)
  2375. return ret;
  2376. }
  2377. /* The display engine is not coherent with the LLC cache on gen6. As
  2378. * a result, we make sure that the pinning that is about to occur is
  2379. * done with uncached PTEs. This is lowest common denominator for all
  2380. * chipsets.
  2381. *
  2382. * However for gen6+, we could do better by using the GFDT bit instead
  2383. * of uncaching, which would allow us to flush all the LLC-cached data
  2384. * with that bit in the PTE to main memory with just one PIPE_CONTROL.
  2385. */
  2386. ret = i915_gem_object_set_cache_level(obj, I915_CACHE_NONE);
  2387. if (ret)
  2388. return ret;
  2389. /* As the user may map the buffer once pinned in the display plane
  2390. * (e.g. libkms for the bootup splash), we have to ensure that we
  2391. * always use map_and_fenceable for all scanout buffers.
  2392. */
  2393. ret = i915_gem_object_pin(obj, alignment, true);
  2394. if (ret)
  2395. return ret;
  2396. i915_gem_object_flush_cpu_write_domain(obj);
  2397. old_write_domain = obj->base.write_domain;
  2398. old_read_domains = obj->base.read_domains;
  2399. /* It should now be out of any other write domains, and we can update
  2400. * the domain values for our changes.
  2401. */
  2402. BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
  2403. obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
  2404. trace_i915_gem_object_change_domain(obj,
  2405. old_read_domains,
  2406. old_write_domain);
  2407. return 0;
  2408. }
  2409. int
  2410. i915_gem_object_finish_gpu(struct drm_i915_gem_object *obj)
  2411. {
  2412. int ret;
  2413. if ((obj->base.read_domains & I915_GEM_GPU_DOMAINS) == 0)
  2414. return 0;
  2415. if (obj->base.write_domain & I915_GEM_GPU_DOMAINS) {
  2416. ret = i915_gem_flush_ring(obj->ring, 0, obj->base.write_domain);
  2417. if (ret)
  2418. return ret;
  2419. }
  2420. ret = i915_gem_object_wait_rendering(obj);
  2421. if (ret)
  2422. return ret;
  2423. /* Ensure that we invalidate the GPU's caches and TLBs. */
  2424. obj->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
  2425. return 0;
  2426. }
  2427. /**
  2428. * Moves a single object to the CPU read, and possibly write domain.
  2429. *
  2430. * This function returns when the move is complete, including waiting on
  2431. * flushes to occur.
  2432. */
  2433. int
  2434. i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
  2435. {
  2436. uint32_t old_write_domain, old_read_domains;
  2437. int ret;
  2438. if (obj->base.write_domain == I915_GEM_DOMAIN_CPU)
  2439. return 0;
  2440. ret = i915_gem_object_flush_gpu_write_domain(obj);
  2441. if (ret)
  2442. return ret;
  2443. if (write || obj->pending_gpu_write) {
  2444. ret = i915_gem_object_wait_rendering(obj);
  2445. if (ret)
  2446. return ret;
  2447. }
  2448. i915_gem_object_flush_gtt_write_domain(obj);
  2449. old_write_domain = obj->base.write_domain;
  2450. old_read_domains = obj->base.read_domains;
  2451. /* Flush the CPU cache if it's still invalid. */
  2452. if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) {
  2453. i915_gem_clflush_object(obj);
  2454. obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
  2455. }
  2456. /* It should now be out of any other write domains, and we can update
  2457. * the domain values for our changes.
  2458. */
  2459. BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
  2460. /* If we're writing through the CPU, then the GPU read domains will
  2461. * need to be invalidated at next use.
  2462. */
  2463. if (write) {
  2464. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  2465. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  2466. }
  2467. trace_i915_gem_object_change_domain(obj,
  2468. old_read_domains,
  2469. old_write_domain);
  2470. return 0;
  2471. }
  2472. /* Throttle our rendering by waiting until the ring has completed our requests
  2473. * emitted over 20 msec ago.
  2474. *
  2475. * Note that if we were to use the current jiffies each time around the loop,
  2476. * we wouldn't escape the function with any frames outstanding if the time to
  2477. * render a frame was over 20ms.
  2478. *
  2479. * This should get us reasonable parallelism between CPU and GPU but also
  2480. * relatively low latency when blocking on a particular request to finish.
  2481. */
  2482. static int
  2483. i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
  2484. {
  2485. struct drm_i915_private *dev_priv = dev->dev_private;
  2486. struct drm_i915_file_private *file_priv = file->driver_priv;
  2487. unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
  2488. struct drm_i915_gem_request *request;
  2489. struct intel_ring_buffer *ring = NULL;
  2490. u32 seqno = 0;
  2491. int ret;
  2492. if (atomic_read(&dev_priv->mm.wedged))
  2493. return -EIO;
  2494. spin_lock(&file_priv->mm.lock);
  2495. list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
  2496. if (time_after_eq(request->emitted_jiffies, recent_enough))
  2497. break;
  2498. ring = request->ring;
  2499. seqno = request->seqno;
  2500. }
  2501. spin_unlock(&file_priv->mm.lock);
  2502. if (seqno == 0)
  2503. return 0;
  2504. ret = __wait_seqno(ring, seqno, true);
  2505. if (ret == 0)
  2506. queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, 0);
  2507. return ret;
  2508. }
  2509. int
  2510. i915_gem_object_pin(struct drm_i915_gem_object *obj,
  2511. uint32_t alignment,
  2512. bool map_and_fenceable)
  2513. {
  2514. int ret;
  2515. BUG_ON(obj->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT);
  2516. if (obj->gtt_space != NULL) {
  2517. if ((alignment && obj->gtt_offset & (alignment - 1)) ||
  2518. (map_and_fenceable && !obj->map_and_fenceable)) {
  2519. WARN(obj->pin_count,
  2520. "bo is already pinned with incorrect alignment:"
  2521. " offset=%x, req.alignment=%x, req.map_and_fenceable=%d,"
  2522. " obj->map_and_fenceable=%d\n",
  2523. obj->gtt_offset, alignment,
  2524. map_and_fenceable,
  2525. obj->map_and_fenceable);
  2526. ret = i915_gem_object_unbind(obj);
  2527. if (ret)
  2528. return ret;
  2529. }
  2530. }
  2531. if (obj->gtt_space == NULL) {
  2532. ret = i915_gem_object_bind_to_gtt(obj, alignment,
  2533. map_and_fenceable);
  2534. if (ret)
  2535. return ret;
  2536. }
  2537. if (!obj->has_global_gtt_mapping && map_and_fenceable)
  2538. i915_gem_gtt_bind_object(obj, obj->cache_level);
  2539. obj->pin_count++;
  2540. obj->pin_mappable |= map_and_fenceable;
  2541. return 0;
  2542. }
  2543. void
  2544. i915_gem_object_unpin(struct drm_i915_gem_object *obj)
  2545. {
  2546. BUG_ON(obj->pin_count == 0);
  2547. BUG_ON(obj->gtt_space == NULL);
  2548. if (--obj->pin_count == 0)
  2549. obj->pin_mappable = false;
  2550. }
  2551. int
  2552. i915_gem_pin_ioctl(struct drm_device *dev, void *data,
  2553. struct drm_file *file)
  2554. {
  2555. struct drm_i915_gem_pin *args = data;
  2556. struct drm_i915_gem_object *obj;
  2557. int ret;
  2558. ret = i915_mutex_lock_interruptible(dev);
  2559. if (ret)
  2560. return ret;
  2561. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  2562. if (&obj->base == NULL) {
  2563. ret = -ENOENT;
  2564. goto unlock;
  2565. }
  2566. if (obj->madv != I915_MADV_WILLNEED) {
  2567. DRM_ERROR("Attempting to pin a purgeable buffer\n");
  2568. ret = -EINVAL;
  2569. goto out;
  2570. }
  2571. if (obj->pin_filp != NULL && obj->pin_filp != file) {
  2572. DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
  2573. args->handle);
  2574. ret = -EINVAL;
  2575. goto out;
  2576. }
  2577. obj->user_pin_count++;
  2578. obj->pin_filp = file;
  2579. if (obj->user_pin_count == 1) {
  2580. ret = i915_gem_object_pin(obj, args->alignment, true);
  2581. if (ret)
  2582. goto out;
  2583. }
  2584. /* XXX - flush the CPU caches for pinned objects
  2585. * as the X server doesn't manage domains yet
  2586. */
  2587. i915_gem_object_flush_cpu_write_domain(obj);
  2588. args->offset = obj->gtt_offset;
  2589. out:
  2590. drm_gem_object_unreference(&obj->base);
  2591. unlock:
  2592. mutex_unlock(&dev->struct_mutex);
  2593. return ret;
  2594. }
  2595. int
  2596. i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
  2597. struct drm_file *file)
  2598. {
  2599. struct drm_i915_gem_pin *args = data;
  2600. struct drm_i915_gem_object *obj;
  2601. int ret;
  2602. ret = i915_mutex_lock_interruptible(dev);
  2603. if (ret)
  2604. return ret;
  2605. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  2606. if (&obj->base == NULL) {
  2607. ret = -ENOENT;
  2608. goto unlock;
  2609. }
  2610. if (obj->pin_filp != file) {
  2611. DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
  2612. args->handle);
  2613. ret = -EINVAL;
  2614. goto out;
  2615. }
  2616. obj->user_pin_count--;
  2617. if (obj->user_pin_count == 0) {
  2618. obj->pin_filp = NULL;
  2619. i915_gem_object_unpin(obj);
  2620. }
  2621. out:
  2622. drm_gem_object_unreference(&obj->base);
  2623. unlock:
  2624. mutex_unlock(&dev->struct_mutex);
  2625. return ret;
  2626. }
  2627. int
  2628. i915_gem_busy_ioctl(struct drm_device *dev, void *data,
  2629. struct drm_file *file)
  2630. {
  2631. struct drm_i915_gem_busy *args = data;
  2632. struct drm_i915_gem_object *obj;
  2633. int ret;
  2634. ret = i915_mutex_lock_interruptible(dev);
  2635. if (ret)
  2636. return ret;
  2637. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  2638. if (&obj->base == NULL) {
  2639. ret = -ENOENT;
  2640. goto unlock;
  2641. }
  2642. /* Count all active objects as busy, even if they are currently not used
  2643. * by the gpu. Users of this interface expect objects to eventually
  2644. * become non-busy without any further actions, therefore emit any
  2645. * necessary flushes here.
  2646. */
  2647. args->busy = obj->active;
  2648. if (args->busy) {
  2649. /* Unconditionally flush objects, even when the gpu still uses this
  2650. * object. Userspace calling this function indicates that it wants to
  2651. * use this buffer rather sooner than later, so issuing the required
  2652. * flush earlier is beneficial.
  2653. */
  2654. if (obj->base.write_domain & I915_GEM_GPU_DOMAINS) {
  2655. ret = i915_gem_flush_ring(obj->ring,
  2656. 0, obj->base.write_domain);
  2657. } else {
  2658. ret = i915_gem_check_olr(obj->ring,
  2659. obj->last_rendering_seqno);
  2660. }
  2661. /* Update the active list for the hardware's current position.
  2662. * Otherwise this only updates on a delayed timer or when irqs
  2663. * are actually unmasked, and our working set ends up being
  2664. * larger than required.
  2665. */
  2666. i915_gem_retire_requests_ring(obj->ring);
  2667. args->busy = obj->active;
  2668. }
  2669. drm_gem_object_unreference(&obj->base);
  2670. unlock:
  2671. mutex_unlock(&dev->struct_mutex);
  2672. return ret;
  2673. }
  2674. int
  2675. i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
  2676. struct drm_file *file_priv)
  2677. {
  2678. return i915_gem_ring_throttle(dev, file_priv);
  2679. }
  2680. int
  2681. i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
  2682. struct drm_file *file_priv)
  2683. {
  2684. struct drm_i915_gem_madvise *args = data;
  2685. struct drm_i915_gem_object *obj;
  2686. int ret;
  2687. switch (args->madv) {
  2688. case I915_MADV_DONTNEED:
  2689. case I915_MADV_WILLNEED:
  2690. break;
  2691. default:
  2692. return -EINVAL;
  2693. }
  2694. ret = i915_mutex_lock_interruptible(dev);
  2695. if (ret)
  2696. return ret;
  2697. obj = to_intel_bo(drm_gem_object_lookup(dev, file_priv, args->handle));
  2698. if (&obj->base == NULL) {
  2699. ret = -ENOENT;
  2700. goto unlock;
  2701. }
  2702. if (obj->pin_count) {
  2703. ret = -EINVAL;
  2704. goto out;
  2705. }
  2706. if (obj->madv != __I915_MADV_PURGED)
  2707. obj->madv = args->madv;
  2708. /* if the object is no longer bound, discard its backing storage */
  2709. if (i915_gem_object_is_purgeable(obj) &&
  2710. obj->gtt_space == NULL)
  2711. i915_gem_object_truncate(obj);
  2712. args->retained = obj->madv != __I915_MADV_PURGED;
  2713. out:
  2714. drm_gem_object_unreference(&obj->base);
  2715. unlock:
  2716. mutex_unlock(&dev->struct_mutex);
  2717. return ret;
  2718. }
  2719. struct drm_i915_gem_object *i915_gem_alloc_object(struct drm_device *dev,
  2720. size_t size)
  2721. {
  2722. struct drm_i915_private *dev_priv = dev->dev_private;
  2723. struct drm_i915_gem_object *obj;
  2724. struct address_space *mapping;
  2725. u32 mask;
  2726. obj = kzalloc(sizeof(*obj), GFP_KERNEL);
  2727. if (obj == NULL)
  2728. return NULL;
  2729. if (drm_gem_object_init(dev, &obj->base, size) != 0) {
  2730. kfree(obj);
  2731. return NULL;
  2732. }
  2733. mask = GFP_HIGHUSER | __GFP_RECLAIMABLE;
  2734. if (IS_CRESTLINE(dev) || IS_BROADWATER(dev)) {
  2735. /* 965gm cannot relocate objects above 4GiB. */
  2736. mask &= ~__GFP_HIGHMEM;
  2737. mask |= __GFP_DMA32;
  2738. }
  2739. mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
  2740. mapping_set_gfp_mask(mapping, mask);
  2741. i915_gem_info_add_obj(dev_priv, size);
  2742. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  2743. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  2744. if (HAS_LLC(dev)) {
  2745. /* On some devices, we can have the GPU use the LLC (the CPU
  2746. * cache) for about a 10% performance improvement
  2747. * compared to uncached. Graphics requests other than
  2748. * display scanout are coherent with the CPU in
  2749. * accessing this cache. This means in this mode we
  2750. * don't need to clflush on the CPU side, and on the
  2751. * GPU side we only need to flush internal caches to
  2752. * get data visible to the CPU.
  2753. *
  2754. * However, we maintain the display planes as UC, and so
  2755. * need to rebind when first used as such.
  2756. */
  2757. obj->cache_level = I915_CACHE_LLC;
  2758. } else
  2759. obj->cache_level = I915_CACHE_NONE;
  2760. obj->base.driver_private = NULL;
  2761. obj->fence_reg = I915_FENCE_REG_NONE;
  2762. INIT_LIST_HEAD(&obj->mm_list);
  2763. INIT_LIST_HEAD(&obj->gtt_list);
  2764. INIT_LIST_HEAD(&obj->ring_list);
  2765. INIT_LIST_HEAD(&obj->exec_list);
  2766. INIT_LIST_HEAD(&obj->gpu_write_list);
  2767. obj->madv = I915_MADV_WILLNEED;
  2768. /* Avoid an unnecessary call to unbind on the first bind. */
  2769. obj->map_and_fenceable = true;
  2770. return obj;
  2771. }
  2772. int i915_gem_init_object(struct drm_gem_object *obj)
  2773. {
  2774. BUG();
  2775. return 0;
  2776. }
  2777. void i915_gem_free_object(struct drm_gem_object *gem_obj)
  2778. {
  2779. struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
  2780. struct drm_device *dev = obj->base.dev;
  2781. drm_i915_private_t *dev_priv = dev->dev_private;
  2782. trace_i915_gem_object_destroy(obj);
  2783. if (gem_obj->import_attach)
  2784. drm_prime_gem_destroy(gem_obj, obj->sg_table);
  2785. if (obj->phys_obj)
  2786. i915_gem_detach_phys_object(dev, obj);
  2787. obj->pin_count = 0;
  2788. if (WARN_ON(i915_gem_object_unbind(obj) == -ERESTARTSYS)) {
  2789. bool was_interruptible;
  2790. was_interruptible = dev_priv->mm.interruptible;
  2791. dev_priv->mm.interruptible = false;
  2792. WARN_ON(i915_gem_object_unbind(obj));
  2793. dev_priv->mm.interruptible = was_interruptible;
  2794. }
  2795. if (obj->base.map_list.map)
  2796. drm_gem_free_mmap_offset(&obj->base);
  2797. drm_gem_object_release(&obj->base);
  2798. i915_gem_info_remove_obj(dev_priv, obj->base.size);
  2799. kfree(obj->bit_17);
  2800. kfree(obj);
  2801. }
  2802. int
  2803. i915_gem_idle(struct drm_device *dev)
  2804. {
  2805. drm_i915_private_t *dev_priv = dev->dev_private;
  2806. int ret;
  2807. mutex_lock(&dev->struct_mutex);
  2808. if (dev_priv->mm.suspended) {
  2809. mutex_unlock(&dev->struct_mutex);
  2810. return 0;
  2811. }
  2812. ret = i915_gpu_idle(dev);
  2813. if (ret) {
  2814. mutex_unlock(&dev->struct_mutex);
  2815. return ret;
  2816. }
  2817. i915_gem_retire_requests(dev);
  2818. /* Under UMS, be paranoid and evict. */
  2819. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2820. i915_gem_evict_everything(dev, false);
  2821. i915_gem_reset_fences(dev);
  2822. /* Hack! Don't let anybody do execbuf while we don't control the chip.
  2823. * We need to replace this with a semaphore, or something.
  2824. * And not confound mm.suspended!
  2825. */
  2826. dev_priv->mm.suspended = 1;
  2827. del_timer_sync(&dev_priv->hangcheck_timer);
  2828. i915_kernel_lost_context(dev);
  2829. i915_gem_cleanup_ringbuffer(dev);
  2830. mutex_unlock(&dev->struct_mutex);
  2831. /* Cancel the retire work handler, which should be idle now. */
  2832. cancel_delayed_work_sync(&dev_priv->mm.retire_work);
  2833. return 0;
  2834. }
  2835. void i915_gem_init_swizzling(struct drm_device *dev)
  2836. {
  2837. drm_i915_private_t *dev_priv = dev->dev_private;
  2838. if (INTEL_INFO(dev)->gen < 5 ||
  2839. dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE)
  2840. return;
  2841. I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
  2842. DISP_TILE_SURFACE_SWIZZLING);
  2843. if (IS_GEN5(dev))
  2844. return;
  2845. I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL);
  2846. if (IS_GEN6(dev))
  2847. I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB));
  2848. else
  2849. I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB));
  2850. }
  2851. void i915_gem_init_ppgtt(struct drm_device *dev)
  2852. {
  2853. drm_i915_private_t *dev_priv = dev->dev_private;
  2854. uint32_t pd_offset;
  2855. struct intel_ring_buffer *ring;
  2856. struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt;
  2857. uint32_t __iomem *pd_addr;
  2858. uint32_t pd_entry;
  2859. int i;
  2860. if (!dev_priv->mm.aliasing_ppgtt)
  2861. return;
  2862. pd_addr = dev_priv->mm.gtt->gtt + ppgtt->pd_offset/sizeof(uint32_t);
  2863. for (i = 0; i < ppgtt->num_pd_entries; i++) {
  2864. dma_addr_t pt_addr;
  2865. if (dev_priv->mm.gtt->needs_dmar)
  2866. pt_addr = ppgtt->pt_dma_addr[i];
  2867. else
  2868. pt_addr = page_to_phys(ppgtt->pt_pages[i]);
  2869. pd_entry = GEN6_PDE_ADDR_ENCODE(pt_addr);
  2870. pd_entry |= GEN6_PDE_VALID;
  2871. writel(pd_entry, pd_addr + i);
  2872. }
  2873. readl(pd_addr);
  2874. pd_offset = ppgtt->pd_offset;
  2875. pd_offset /= 64; /* in cachelines, */
  2876. pd_offset <<= 16;
  2877. if (INTEL_INFO(dev)->gen == 6) {
  2878. uint32_t ecochk, gab_ctl, ecobits;
  2879. ecobits = I915_READ(GAC_ECO_BITS);
  2880. I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_PPGTT_CACHE64B);
  2881. gab_ctl = I915_READ(GAB_CTL);
  2882. I915_WRITE(GAB_CTL, gab_ctl | GAB_CTL_CONT_AFTER_PAGEFAULT);
  2883. ecochk = I915_READ(GAM_ECOCHK);
  2884. I915_WRITE(GAM_ECOCHK, ecochk | ECOCHK_SNB_BIT |
  2885. ECOCHK_PPGTT_CACHE64B);
  2886. I915_WRITE(GFX_MODE, _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
  2887. } else if (INTEL_INFO(dev)->gen >= 7) {
  2888. I915_WRITE(GAM_ECOCHK, ECOCHK_PPGTT_CACHE64B);
  2889. /* GFX_MODE is per-ring on gen7+ */
  2890. }
  2891. for_each_ring(ring, dev_priv, i) {
  2892. if (INTEL_INFO(dev)->gen >= 7)
  2893. I915_WRITE(RING_MODE_GEN7(ring),
  2894. _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
  2895. I915_WRITE(RING_PP_DIR_DCLV(ring), PP_DIR_DCLV_2G);
  2896. I915_WRITE(RING_PP_DIR_BASE(ring), pd_offset);
  2897. }
  2898. }
  2899. int
  2900. i915_gem_init_hw(struct drm_device *dev)
  2901. {
  2902. drm_i915_private_t *dev_priv = dev->dev_private;
  2903. int ret;
  2904. i915_gem_init_swizzling(dev);
  2905. ret = intel_init_render_ring_buffer(dev);
  2906. if (ret)
  2907. return ret;
  2908. if (HAS_BSD(dev)) {
  2909. ret = intel_init_bsd_ring_buffer(dev);
  2910. if (ret)
  2911. goto cleanup_render_ring;
  2912. }
  2913. if (HAS_BLT(dev)) {
  2914. ret = intel_init_blt_ring_buffer(dev);
  2915. if (ret)
  2916. goto cleanup_bsd_ring;
  2917. }
  2918. dev_priv->next_seqno = 1;
  2919. i915_gem_init_ppgtt(dev);
  2920. return 0;
  2921. cleanup_bsd_ring:
  2922. intel_cleanup_ring_buffer(&dev_priv->ring[VCS]);
  2923. cleanup_render_ring:
  2924. intel_cleanup_ring_buffer(&dev_priv->ring[RCS]);
  2925. return ret;
  2926. }
  2927. static bool
  2928. intel_enable_ppgtt(struct drm_device *dev)
  2929. {
  2930. if (i915_enable_ppgtt >= 0)
  2931. return i915_enable_ppgtt;
  2932. #ifdef CONFIG_INTEL_IOMMU
  2933. /* Disable ppgtt on SNB if VT-d is on. */
  2934. if (INTEL_INFO(dev)->gen == 6 && intel_iommu_gfx_mapped)
  2935. return false;
  2936. #endif
  2937. return true;
  2938. }
  2939. int i915_gem_init(struct drm_device *dev)
  2940. {
  2941. struct drm_i915_private *dev_priv = dev->dev_private;
  2942. unsigned long gtt_size, mappable_size;
  2943. int ret;
  2944. gtt_size = dev_priv->mm.gtt->gtt_total_entries << PAGE_SHIFT;
  2945. mappable_size = dev_priv->mm.gtt->gtt_mappable_entries << PAGE_SHIFT;
  2946. mutex_lock(&dev->struct_mutex);
  2947. if (intel_enable_ppgtt(dev) && HAS_ALIASING_PPGTT(dev)) {
  2948. /* PPGTT pdes are stolen from global gtt ptes, so shrink the
  2949. * aperture accordingly when using aliasing ppgtt. */
  2950. gtt_size -= I915_PPGTT_PD_ENTRIES*PAGE_SIZE;
  2951. i915_gem_init_global_gtt(dev, 0, mappable_size, gtt_size);
  2952. ret = i915_gem_init_aliasing_ppgtt(dev);
  2953. if (ret) {
  2954. mutex_unlock(&dev->struct_mutex);
  2955. return ret;
  2956. }
  2957. } else {
  2958. /* Let GEM Manage all of the aperture.
  2959. *
  2960. * However, leave one page at the end still bound to the scratch
  2961. * page. There are a number of places where the hardware
  2962. * apparently prefetches past the end of the object, and we've
  2963. * seen multiple hangs with the GPU head pointer stuck in a
  2964. * batchbuffer bound at the last page of the aperture. One page
  2965. * should be enough to keep any prefetching inside of the
  2966. * aperture.
  2967. */
  2968. i915_gem_init_global_gtt(dev, 0, mappable_size,
  2969. gtt_size);
  2970. }
  2971. ret = i915_gem_init_hw(dev);
  2972. mutex_unlock(&dev->struct_mutex);
  2973. if (ret) {
  2974. i915_gem_cleanup_aliasing_ppgtt(dev);
  2975. return ret;
  2976. }
  2977. /* Allow hardware batchbuffers unless told otherwise, but not for KMS. */
  2978. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2979. dev_priv->dri1.allow_batchbuffer = 1;
  2980. return 0;
  2981. }
  2982. void
  2983. i915_gem_cleanup_ringbuffer(struct drm_device *dev)
  2984. {
  2985. drm_i915_private_t *dev_priv = dev->dev_private;
  2986. struct intel_ring_buffer *ring;
  2987. int i;
  2988. for_each_ring(ring, dev_priv, i)
  2989. intel_cleanup_ring_buffer(ring);
  2990. }
  2991. int
  2992. i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
  2993. struct drm_file *file_priv)
  2994. {
  2995. drm_i915_private_t *dev_priv = dev->dev_private;
  2996. int ret;
  2997. if (drm_core_check_feature(dev, DRIVER_MODESET))
  2998. return 0;
  2999. if (atomic_read(&dev_priv->mm.wedged)) {
  3000. DRM_ERROR("Reenabling wedged hardware, good luck\n");
  3001. atomic_set(&dev_priv->mm.wedged, 0);
  3002. }
  3003. mutex_lock(&dev->struct_mutex);
  3004. dev_priv->mm.suspended = 0;
  3005. ret = i915_gem_init_hw(dev);
  3006. if (ret != 0) {
  3007. mutex_unlock(&dev->struct_mutex);
  3008. return ret;
  3009. }
  3010. BUG_ON(!list_empty(&dev_priv->mm.active_list));
  3011. BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
  3012. BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
  3013. mutex_unlock(&dev->struct_mutex);
  3014. ret = drm_irq_install(dev);
  3015. if (ret)
  3016. goto cleanup_ringbuffer;
  3017. return 0;
  3018. cleanup_ringbuffer:
  3019. mutex_lock(&dev->struct_mutex);
  3020. i915_gem_cleanup_ringbuffer(dev);
  3021. dev_priv->mm.suspended = 1;
  3022. mutex_unlock(&dev->struct_mutex);
  3023. return ret;
  3024. }
  3025. int
  3026. i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
  3027. struct drm_file *file_priv)
  3028. {
  3029. if (drm_core_check_feature(dev, DRIVER_MODESET))
  3030. return 0;
  3031. drm_irq_uninstall(dev);
  3032. return i915_gem_idle(dev);
  3033. }
  3034. void
  3035. i915_gem_lastclose(struct drm_device *dev)
  3036. {
  3037. int ret;
  3038. if (drm_core_check_feature(dev, DRIVER_MODESET))
  3039. return;
  3040. ret = i915_gem_idle(dev);
  3041. if (ret)
  3042. DRM_ERROR("failed to idle hardware: %d\n", ret);
  3043. }
  3044. static void
  3045. init_ring_lists(struct intel_ring_buffer *ring)
  3046. {
  3047. INIT_LIST_HEAD(&ring->active_list);
  3048. INIT_LIST_HEAD(&ring->request_list);
  3049. INIT_LIST_HEAD(&ring->gpu_write_list);
  3050. }
  3051. void
  3052. i915_gem_load(struct drm_device *dev)
  3053. {
  3054. int i;
  3055. drm_i915_private_t *dev_priv = dev->dev_private;
  3056. INIT_LIST_HEAD(&dev_priv->mm.active_list);
  3057. INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
  3058. INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
  3059. INIT_LIST_HEAD(&dev_priv->mm.fence_list);
  3060. INIT_LIST_HEAD(&dev_priv->mm.gtt_list);
  3061. for (i = 0; i < I915_NUM_RINGS; i++)
  3062. init_ring_lists(&dev_priv->ring[i]);
  3063. for (i = 0; i < I915_MAX_NUM_FENCES; i++)
  3064. INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
  3065. INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
  3066. i915_gem_retire_work_handler);
  3067. init_completion(&dev_priv->error_completion);
  3068. /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
  3069. if (IS_GEN3(dev)) {
  3070. I915_WRITE(MI_ARB_STATE,
  3071. _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
  3072. }
  3073. dev_priv->relative_constants_mode = I915_EXEC_CONSTANTS_REL_GENERAL;
  3074. /* Old X drivers will take 0-2 for front, back, depth buffers */
  3075. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  3076. dev_priv->fence_reg_start = 3;
  3077. if (INTEL_INFO(dev)->gen >= 4 || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
  3078. dev_priv->num_fence_regs = 16;
  3079. else
  3080. dev_priv->num_fence_regs = 8;
  3081. /* Initialize fence registers to zero */
  3082. i915_gem_reset_fences(dev);
  3083. i915_gem_detect_bit_6_swizzle(dev);
  3084. init_waitqueue_head(&dev_priv->pending_flip_queue);
  3085. dev_priv->mm.interruptible = true;
  3086. dev_priv->mm.inactive_shrinker.shrink = i915_gem_inactive_shrink;
  3087. dev_priv->mm.inactive_shrinker.seeks = DEFAULT_SEEKS;
  3088. register_shrinker(&dev_priv->mm.inactive_shrinker);
  3089. }
  3090. /*
  3091. * Create a physically contiguous memory object for this object
  3092. * e.g. for cursor + overlay regs
  3093. */
  3094. static int i915_gem_init_phys_object(struct drm_device *dev,
  3095. int id, int size, int align)
  3096. {
  3097. drm_i915_private_t *dev_priv = dev->dev_private;
  3098. struct drm_i915_gem_phys_object *phys_obj;
  3099. int ret;
  3100. if (dev_priv->mm.phys_objs[id - 1] || !size)
  3101. return 0;
  3102. phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
  3103. if (!phys_obj)
  3104. return -ENOMEM;
  3105. phys_obj->id = id;
  3106. phys_obj->handle = drm_pci_alloc(dev, size, align);
  3107. if (!phys_obj->handle) {
  3108. ret = -ENOMEM;
  3109. goto kfree_obj;
  3110. }
  3111. #ifdef CONFIG_X86
  3112. set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
  3113. #endif
  3114. dev_priv->mm.phys_objs[id - 1] = phys_obj;
  3115. return 0;
  3116. kfree_obj:
  3117. kfree(phys_obj);
  3118. return ret;
  3119. }
  3120. static void i915_gem_free_phys_object(struct drm_device *dev, int id)
  3121. {
  3122. drm_i915_private_t *dev_priv = dev->dev_private;
  3123. struct drm_i915_gem_phys_object *phys_obj;
  3124. if (!dev_priv->mm.phys_objs[id - 1])
  3125. return;
  3126. phys_obj = dev_priv->mm.phys_objs[id - 1];
  3127. if (phys_obj->cur_obj) {
  3128. i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
  3129. }
  3130. #ifdef CONFIG_X86
  3131. set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
  3132. #endif
  3133. drm_pci_free(dev, phys_obj->handle);
  3134. kfree(phys_obj);
  3135. dev_priv->mm.phys_objs[id - 1] = NULL;
  3136. }
  3137. void i915_gem_free_all_phys_object(struct drm_device *dev)
  3138. {
  3139. int i;
  3140. for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
  3141. i915_gem_free_phys_object(dev, i);
  3142. }
  3143. void i915_gem_detach_phys_object(struct drm_device *dev,
  3144. struct drm_i915_gem_object *obj)
  3145. {
  3146. struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
  3147. char *vaddr;
  3148. int i;
  3149. int page_count;
  3150. if (!obj->phys_obj)
  3151. return;
  3152. vaddr = obj->phys_obj->handle->vaddr;
  3153. page_count = obj->base.size / PAGE_SIZE;
  3154. for (i = 0; i < page_count; i++) {
  3155. struct page *page = shmem_read_mapping_page(mapping, i);
  3156. if (!IS_ERR(page)) {
  3157. char *dst = kmap_atomic(page);
  3158. memcpy(dst, vaddr + i*PAGE_SIZE, PAGE_SIZE);
  3159. kunmap_atomic(dst);
  3160. drm_clflush_pages(&page, 1);
  3161. set_page_dirty(page);
  3162. mark_page_accessed(page);
  3163. page_cache_release(page);
  3164. }
  3165. }
  3166. intel_gtt_chipset_flush();
  3167. obj->phys_obj->cur_obj = NULL;
  3168. obj->phys_obj = NULL;
  3169. }
  3170. int
  3171. i915_gem_attach_phys_object(struct drm_device *dev,
  3172. struct drm_i915_gem_object *obj,
  3173. int id,
  3174. int align)
  3175. {
  3176. struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
  3177. drm_i915_private_t *dev_priv = dev->dev_private;
  3178. int ret = 0;
  3179. int page_count;
  3180. int i;
  3181. if (id > I915_MAX_PHYS_OBJECT)
  3182. return -EINVAL;
  3183. if (obj->phys_obj) {
  3184. if (obj->phys_obj->id == id)
  3185. return 0;
  3186. i915_gem_detach_phys_object(dev, obj);
  3187. }
  3188. /* create a new object */
  3189. if (!dev_priv->mm.phys_objs[id - 1]) {
  3190. ret = i915_gem_init_phys_object(dev, id,
  3191. obj->base.size, align);
  3192. if (ret) {
  3193. DRM_ERROR("failed to init phys object %d size: %zu\n",
  3194. id, obj->base.size);
  3195. return ret;
  3196. }
  3197. }
  3198. /* bind to the object */
  3199. obj->phys_obj = dev_priv->mm.phys_objs[id - 1];
  3200. obj->phys_obj->cur_obj = obj;
  3201. page_count = obj->base.size / PAGE_SIZE;
  3202. for (i = 0; i < page_count; i++) {
  3203. struct page *page;
  3204. char *dst, *src;
  3205. page = shmem_read_mapping_page(mapping, i);
  3206. if (IS_ERR(page))
  3207. return PTR_ERR(page);
  3208. src = kmap_atomic(page);
  3209. dst = obj->phys_obj->handle->vaddr + (i * PAGE_SIZE);
  3210. memcpy(dst, src, PAGE_SIZE);
  3211. kunmap_atomic(src);
  3212. mark_page_accessed(page);
  3213. page_cache_release(page);
  3214. }
  3215. return 0;
  3216. }
  3217. static int
  3218. i915_gem_phys_pwrite(struct drm_device *dev,
  3219. struct drm_i915_gem_object *obj,
  3220. struct drm_i915_gem_pwrite *args,
  3221. struct drm_file *file_priv)
  3222. {
  3223. void *vaddr = obj->phys_obj->handle->vaddr + args->offset;
  3224. char __user *user_data = (char __user *) (uintptr_t) args->data_ptr;
  3225. if (__copy_from_user_inatomic_nocache(vaddr, user_data, args->size)) {
  3226. unsigned long unwritten;
  3227. /* The physical object once assigned is fixed for the lifetime
  3228. * of the obj, so we can safely drop the lock and continue
  3229. * to access vaddr.
  3230. */
  3231. mutex_unlock(&dev->struct_mutex);
  3232. unwritten = copy_from_user(vaddr, user_data, args->size);
  3233. mutex_lock(&dev->struct_mutex);
  3234. if (unwritten)
  3235. return -EFAULT;
  3236. }
  3237. intel_gtt_chipset_flush();
  3238. return 0;
  3239. }
  3240. void i915_gem_release(struct drm_device *dev, struct drm_file *file)
  3241. {
  3242. struct drm_i915_file_private *file_priv = file->driver_priv;
  3243. /* Clean up our request list when the client is going away, so that
  3244. * later retire_requests won't dereference our soon-to-be-gone
  3245. * file_priv.
  3246. */
  3247. spin_lock(&file_priv->mm.lock);
  3248. while (!list_empty(&file_priv->mm.request_list)) {
  3249. struct drm_i915_gem_request *request;
  3250. request = list_first_entry(&file_priv->mm.request_list,
  3251. struct drm_i915_gem_request,
  3252. client_list);
  3253. list_del(&request->client_list);
  3254. request->file_priv = NULL;
  3255. }
  3256. spin_unlock(&file_priv->mm.lock);
  3257. }
  3258. static int
  3259. i915_gpu_is_active(struct drm_device *dev)
  3260. {
  3261. drm_i915_private_t *dev_priv = dev->dev_private;
  3262. int lists_empty;
  3263. lists_empty = list_empty(&dev_priv->mm.flushing_list) &&
  3264. list_empty(&dev_priv->mm.active_list);
  3265. return !lists_empty;
  3266. }
  3267. static int
  3268. i915_gem_inactive_shrink(struct shrinker *shrinker, struct shrink_control *sc)
  3269. {
  3270. struct drm_i915_private *dev_priv =
  3271. container_of(shrinker,
  3272. struct drm_i915_private,
  3273. mm.inactive_shrinker);
  3274. struct drm_device *dev = dev_priv->dev;
  3275. struct drm_i915_gem_object *obj, *next;
  3276. int nr_to_scan = sc->nr_to_scan;
  3277. int cnt;
  3278. if (!mutex_trylock(&dev->struct_mutex))
  3279. return 0;
  3280. /* "fast-path" to count number of available objects */
  3281. if (nr_to_scan == 0) {
  3282. cnt = 0;
  3283. list_for_each_entry(obj,
  3284. &dev_priv->mm.inactive_list,
  3285. mm_list)
  3286. cnt++;
  3287. mutex_unlock(&dev->struct_mutex);
  3288. return cnt / 100 * sysctl_vfs_cache_pressure;
  3289. }
  3290. rescan:
  3291. /* first scan for clean buffers */
  3292. i915_gem_retire_requests(dev);
  3293. list_for_each_entry_safe(obj, next,
  3294. &dev_priv->mm.inactive_list,
  3295. mm_list) {
  3296. if (i915_gem_object_is_purgeable(obj)) {
  3297. if (i915_gem_object_unbind(obj) == 0 &&
  3298. --nr_to_scan == 0)
  3299. break;
  3300. }
  3301. }
  3302. /* second pass, evict/count anything still on the inactive list */
  3303. cnt = 0;
  3304. list_for_each_entry_safe(obj, next,
  3305. &dev_priv->mm.inactive_list,
  3306. mm_list) {
  3307. if (nr_to_scan &&
  3308. i915_gem_object_unbind(obj) == 0)
  3309. nr_to_scan--;
  3310. else
  3311. cnt++;
  3312. }
  3313. if (nr_to_scan && i915_gpu_is_active(dev)) {
  3314. /*
  3315. * We are desperate for pages, so as a last resort, wait
  3316. * for the GPU to finish and discard whatever we can.
  3317. * This has a dramatic impact to reduce the number of
  3318. * OOM-killer events whilst running the GPU aggressively.
  3319. */
  3320. if (i915_gpu_idle(dev) == 0)
  3321. goto rescan;
  3322. }
  3323. mutex_unlock(&dev->struct_mutex);
  3324. return cnt / 100 * sysctl_vfs_cache_pressure;
  3325. }