i915_gem.c 124 KB

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