i915_gem.c 133 KB

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