i915_gem.c 111 KB

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