i915_gem.c 110 KB

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