i915_gem.c 127 KB

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