i915_gem.c 156 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870
  1. /*
  2. * Copyright © 2008-2015 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_gem_clflush.h"
  32. #include "i915_vgpu.h"
  33. #include "i915_trace.h"
  34. #include "intel_drv.h"
  35. #include "intel_frontbuffer.h"
  36. #include "intel_mocs.h"
  37. #include "i915_gemfs.h"
  38. #include <linux/dma-fence-array.h>
  39. #include <linux/kthread.h>
  40. #include <linux/reservation.h>
  41. #include <linux/shmem_fs.h>
  42. #include <linux/slab.h>
  43. #include <linux/stop_machine.h>
  44. #include <linux/swap.h>
  45. #include <linux/pci.h>
  46. #include <linux/dma-buf.h>
  47. static void i915_gem_flush_free_objects(struct drm_i915_private *i915);
  48. static bool cpu_write_needs_clflush(struct drm_i915_gem_object *obj)
  49. {
  50. if (obj->cache_dirty)
  51. return false;
  52. if (!(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE))
  53. return true;
  54. return obj->pin_global; /* currently in use by HW, keep flushed */
  55. }
  56. static int
  57. insert_mappable_node(struct i915_ggtt *ggtt,
  58. struct drm_mm_node *node, u32 size)
  59. {
  60. memset(node, 0, sizeof(*node));
  61. return drm_mm_insert_node_in_range(&ggtt->base.mm, node,
  62. size, 0, I915_COLOR_UNEVICTABLE,
  63. 0, ggtt->mappable_end,
  64. DRM_MM_INSERT_LOW);
  65. }
  66. static void
  67. remove_mappable_node(struct drm_mm_node *node)
  68. {
  69. drm_mm_remove_node(node);
  70. }
  71. /* some bookkeeping */
  72. static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv,
  73. u64 size)
  74. {
  75. spin_lock(&dev_priv->mm.object_stat_lock);
  76. dev_priv->mm.object_count++;
  77. dev_priv->mm.object_memory += size;
  78. spin_unlock(&dev_priv->mm.object_stat_lock);
  79. }
  80. static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
  81. u64 size)
  82. {
  83. spin_lock(&dev_priv->mm.object_stat_lock);
  84. dev_priv->mm.object_count--;
  85. dev_priv->mm.object_memory -= size;
  86. spin_unlock(&dev_priv->mm.object_stat_lock);
  87. }
  88. static int
  89. i915_gem_wait_for_error(struct i915_gpu_error *error)
  90. {
  91. int ret;
  92. might_sleep();
  93. /*
  94. * Only wait 10 seconds for the gpu reset to complete to avoid hanging
  95. * userspace. If it takes that long something really bad is going on and
  96. * we should simply try to bail out and fail as gracefully as possible.
  97. */
  98. ret = wait_event_interruptible_timeout(error->reset_queue,
  99. !i915_reset_backoff(error),
  100. I915_RESET_TIMEOUT);
  101. if (ret == 0) {
  102. DRM_ERROR("Timed out waiting for the gpu reset to complete\n");
  103. return -EIO;
  104. } else if (ret < 0) {
  105. return ret;
  106. } else {
  107. return 0;
  108. }
  109. }
  110. int i915_mutex_lock_interruptible(struct drm_device *dev)
  111. {
  112. struct drm_i915_private *dev_priv = to_i915(dev);
  113. int ret;
  114. ret = i915_gem_wait_for_error(&dev_priv->gpu_error);
  115. if (ret)
  116. return ret;
  117. ret = mutex_lock_interruptible(&dev->struct_mutex);
  118. if (ret)
  119. return ret;
  120. return 0;
  121. }
  122. int
  123. i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
  124. struct drm_file *file)
  125. {
  126. struct drm_i915_private *dev_priv = to_i915(dev);
  127. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  128. struct drm_i915_gem_get_aperture *args = data;
  129. struct i915_vma *vma;
  130. u64 pinned;
  131. pinned = ggtt->base.reserved;
  132. mutex_lock(&dev->struct_mutex);
  133. list_for_each_entry(vma, &ggtt->base.active_list, vm_link)
  134. if (i915_vma_is_pinned(vma))
  135. pinned += vma->node.size;
  136. list_for_each_entry(vma, &ggtt->base.inactive_list, vm_link)
  137. if (i915_vma_is_pinned(vma))
  138. pinned += vma->node.size;
  139. mutex_unlock(&dev->struct_mutex);
  140. args->aper_size = ggtt->base.total;
  141. args->aper_available_size = args->aper_size - pinned;
  142. return 0;
  143. }
  144. static int i915_gem_object_get_pages_phys(struct drm_i915_gem_object *obj)
  145. {
  146. struct address_space *mapping = obj->base.filp->f_mapping;
  147. drm_dma_handle_t *phys;
  148. struct sg_table *st;
  149. struct scatterlist *sg;
  150. char *vaddr;
  151. int i;
  152. int err;
  153. if (WARN_ON(i915_gem_object_needs_bit17_swizzle(obj)))
  154. return -EINVAL;
  155. /* Always aligning to the object size, allows a single allocation
  156. * to handle all possible callers, and given typical object sizes,
  157. * the alignment of the buddy allocation will naturally match.
  158. */
  159. phys = drm_pci_alloc(obj->base.dev,
  160. roundup_pow_of_two(obj->base.size),
  161. roundup_pow_of_two(obj->base.size));
  162. if (!phys)
  163. return -ENOMEM;
  164. vaddr = phys->vaddr;
  165. for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
  166. struct page *page;
  167. char *src;
  168. page = shmem_read_mapping_page(mapping, i);
  169. if (IS_ERR(page)) {
  170. err = PTR_ERR(page);
  171. goto err_phys;
  172. }
  173. src = kmap_atomic(page);
  174. memcpy(vaddr, src, PAGE_SIZE);
  175. drm_clflush_virt_range(vaddr, PAGE_SIZE);
  176. kunmap_atomic(src);
  177. put_page(page);
  178. vaddr += PAGE_SIZE;
  179. }
  180. i915_gem_chipset_flush(to_i915(obj->base.dev));
  181. st = kmalloc(sizeof(*st), GFP_KERNEL);
  182. if (!st) {
  183. err = -ENOMEM;
  184. goto err_phys;
  185. }
  186. if (sg_alloc_table(st, 1, GFP_KERNEL)) {
  187. kfree(st);
  188. err = -ENOMEM;
  189. goto err_phys;
  190. }
  191. sg = st->sgl;
  192. sg->offset = 0;
  193. sg->length = obj->base.size;
  194. sg_dma_address(sg) = phys->busaddr;
  195. sg_dma_len(sg) = obj->base.size;
  196. obj->phys_handle = phys;
  197. __i915_gem_object_set_pages(obj, st, sg->length);
  198. return 0;
  199. err_phys:
  200. drm_pci_free(obj->base.dev, phys);
  201. return err;
  202. }
  203. static void __start_cpu_write(struct drm_i915_gem_object *obj)
  204. {
  205. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  206. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  207. if (cpu_write_needs_clflush(obj))
  208. obj->cache_dirty = true;
  209. }
  210. static void
  211. __i915_gem_object_release_shmem(struct drm_i915_gem_object *obj,
  212. struct sg_table *pages,
  213. bool needs_clflush)
  214. {
  215. GEM_BUG_ON(obj->mm.madv == __I915_MADV_PURGED);
  216. if (obj->mm.madv == I915_MADV_DONTNEED)
  217. obj->mm.dirty = false;
  218. if (needs_clflush &&
  219. (obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0 &&
  220. !(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ))
  221. drm_clflush_sg(pages);
  222. __start_cpu_write(obj);
  223. }
  224. static void
  225. i915_gem_object_put_pages_phys(struct drm_i915_gem_object *obj,
  226. struct sg_table *pages)
  227. {
  228. __i915_gem_object_release_shmem(obj, pages, false);
  229. if (obj->mm.dirty) {
  230. struct address_space *mapping = obj->base.filp->f_mapping;
  231. char *vaddr = obj->phys_handle->vaddr;
  232. int i;
  233. for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
  234. struct page *page;
  235. char *dst;
  236. page = shmem_read_mapping_page(mapping, i);
  237. if (IS_ERR(page))
  238. continue;
  239. dst = kmap_atomic(page);
  240. drm_clflush_virt_range(vaddr, PAGE_SIZE);
  241. memcpy(dst, vaddr, PAGE_SIZE);
  242. kunmap_atomic(dst);
  243. set_page_dirty(page);
  244. if (obj->mm.madv == I915_MADV_WILLNEED)
  245. mark_page_accessed(page);
  246. put_page(page);
  247. vaddr += PAGE_SIZE;
  248. }
  249. obj->mm.dirty = false;
  250. }
  251. sg_free_table(pages);
  252. kfree(pages);
  253. drm_pci_free(obj->base.dev, obj->phys_handle);
  254. }
  255. static void
  256. i915_gem_object_release_phys(struct drm_i915_gem_object *obj)
  257. {
  258. i915_gem_object_unpin_pages(obj);
  259. }
  260. static const struct drm_i915_gem_object_ops i915_gem_phys_ops = {
  261. .get_pages = i915_gem_object_get_pages_phys,
  262. .put_pages = i915_gem_object_put_pages_phys,
  263. .release = i915_gem_object_release_phys,
  264. };
  265. static const struct drm_i915_gem_object_ops i915_gem_object_ops;
  266. int i915_gem_object_unbind(struct drm_i915_gem_object *obj)
  267. {
  268. struct i915_vma *vma;
  269. LIST_HEAD(still_in_list);
  270. int ret;
  271. lockdep_assert_held(&obj->base.dev->struct_mutex);
  272. /* Closed vma are removed from the obj->vma_list - but they may
  273. * still have an active binding on the object. To remove those we
  274. * must wait for all rendering to complete to the object (as unbinding
  275. * must anyway), and retire the requests.
  276. */
  277. ret = i915_gem_object_set_to_cpu_domain(obj, false);
  278. if (ret)
  279. return ret;
  280. while ((vma = list_first_entry_or_null(&obj->vma_list,
  281. struct i915_vma,
  282. obj_link))) {
  283. list_move_tail(&vma->obj_link, &still_in_list);
  284. ret = i915_vma_unbind(vma);
  285. if (ret)
  286. break;
  287. }
  288. list_splice(&still_in_list, &obj->vma_list);
  289. return ret;
  290. }
  291. static long
  292. i915_gem_object_wait_fence(struct dma_fence *fence,
  293. unsigned int flags,
  294. long timeout,
  295. struct intel_rps_client *rps_client)
  296. {
  297. struct drm_i915_gem_request *rq;
  298. BUILD_BUG_ON(I915_WAIT_INTERRUPTIBLE != 0x1);
  299. if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
  300. return timeout;
  301. if (!dma_fence_is_i915(fence))
  302. return dma_fence_wait_timeout(fence,
  303. flags & I915_WAIT_INTERRUPTIBLE,
  304. timeout);
  305. rq = to_request(fence);
  306. if (i915_gem_request_completed(rq))
  307. goto out;
  308. /* This client is about to stall waiting for the GPU. In many cases
  309. * this is undesirable and limits the throughput of the system, as
  310. * many clients cannot continue processing user input/output whilst
  311. * blocked. RPS autotuning may take tens of milliseconds to respond
  312. * to the GPU load and thus incurs additional latency for the client.
  313. * We can circumvent that by promoting the GPU frequency to maximum
  314. * before we wait. This makes the GPU throttle up much more quickly
  315. * (good for benchmarks and user experience, e.g. window animations),
  316. * but at a cost of spending more power processing the workload
  317. * (bad for battery). Not all clients even want their results
  318. * immediately and for them we should just let the GPU select its own
  319. * frequency to maximise efficiency. To prevent a single client from
  320. * forcing the clocks too high for the whole system, we only allow
  321. * each client to waitboost once in a busy period.
  322. */
  323. if (rps_client) {
  324. if (INTEL_GEN(rq->i915) >= 6)
  325. gen6_rps_boost(rq, rps_client);
  326. else
  327. rps_client = NULL;
  328. }
  329. timeout = i915_wait_request(rq, flags, timeout);
  330. out:
  331. if (flags & I915_WAIT_LOCKED && i915_gem_request_completed(rq))
  332. i915_gem_request_retire_upto(rq);
  333. return timeout;
  334. }
  335. static long
  336. i915_gem_object_wait_reservation(struct reservation_object *resv,
  337. unsigned int flags,
  338. long timeout,
  339. struct intel_rps_client *rps_client)
  340. {
  341. unsigned int seq = __read_seqcount_begin(&resv->seq);
  342. struct dma_fence *excl;
  343. bool prune_fences = false;
  344. if (flags & I915_WAIT_ALL) {
  345. struct dma_fence **shared;
  346. unsigned int count, i;
  347. int ret;
  348. ret = reservation_object_get_fences_rcu(resv,
  349. &excl, &count, &shared);
  350. if (ret)
  351. return ret;
  352. for (i = 0; i < count; i++) {
  353. timeout = i915_gem_object_wait_fence(shared[i],
  354. flags, timeout,
  355. rps_client);
  356. if (timeout < 0)
  357. break;
  358. dma_fence_put(shared[i]);
  359. }
  360. for (; i < count; i++)
  361. dma_fence_put(shared[i]);
  362. kfree(shared);
  363. prune_fences = count && timeout >= 0;
  364. } else {
  365. excl = reservation_object_get_excl_rcu(resv);
  366. }
  367. if (excl && timeout >= 0) {
  368. timeout = i915_gem_object_wait_fence(excl, flags, timeout,
  369. rps_client);
  370. prune_fences = timeout >= 0;
  371. }
  372. dma_fence_put(excl);
  373. /* Oportunistically prune the fences iff we know they have *all* been
  374. * signaled and that the reservation object has not been changed (i.e.
  375. * no new fences have been added).
  376. */
  377. if (prune_fences && !__read_seqcount_retry(&resv->seq, seq)) {
  378. if (reservation_object_trylock(resv)) {
  379. if (!__read_seqcount_retry(&resv->seq, seq))
  380. reservation_object_add_excl_fence(resv, NULL);
  381. reservation_object_unlock(resv);
  382. }
  383. }
  384. return timeout;
  385. }
  386. static void __fence_set_priority(struct dma_fence *fence, int prio)
  387. {
  388. struct drm_i915_gem_request *rq;
  389. struct intel_engine_cs *engine;
  390. if (dma_fence_is_signaled(fence) || !dma_fence_is_i915(fence))
  391. return;
  392. rq = to_request(fence);
  393. engine = rq->engine;
  394. if (!engine->schedule)
  395. return;
  396. engine->schedule(rq, prio);
  397. }
  398. static void fence_set_priority(struct dma_fence *fence, int prio)
  399. {
  400. /* Recurse once into a fence-array */
  401. if (dma_fence_is_array(fence)) {
  402. struct dma_fence_array *array = to_dma_fence_array(fence);
  403. int i;
  404. for (i = 0; i < array->num_fences; i++)
  405. __fence_set_priority(array->fences[i], prio);
  406. } else {
  407. __fence_set_priority(fence, prio);
  408. }
  409. }
  410. int
  411. i915_gem_object_wait_priority(struct drm_i915_gem_object *obj,
  412. unsigned int flags,
  413. int prio)
  414. {
  415. struct dma_fence *excl;
  416. if (flags & I915_WAIT_ALL) {
  417. struct dma_fence **shared;
  418. unsigned int count, i;
  419. int ret;
  420. ret = reservation_object_get_fences_rcu(obj->resv,
  421. &excl, &count, &shared);
  422. if (ret)
  423. return ret;
  424. for (i = 0; i < count; i++) {
  425. fence_set_priority(shared[i], prio);
  426. dma_fence_put(shared[i]);
  427. }
  428. kfree(shared);
  429. } else {
  430. excl = reservation_object_get_excl_rcu(obj->resv);
  431. }
  432. if (excl) {
  433. fence_set_priority(excl, prio);
  434. dma_fence_put(excl);
  435. }
  436. return 0;
  437. }
  438. /**
  439. * Waits for rendering to the object to be completed
  440. * @obj: i915 gem object
  441. * @flags: how to wait (under a lock, for all rendering or just for writes etc)
  442. * @timeout: how long to wait
  443. * @rps_client: client (user process) to charge for any waitboosting
  444. */
  445. int
  446. i915_gem_object_wait(struct drm_i915_gem_object *obj,
  447. unsigned int flags,
  448. long timeout,
  449. struct intel_rps_client *rps_client)
  450. {
  451. might_sleep();
  452. #if IS_ENABLED(CONFIG_LOCKDEP)
  453. GEM_BUG_ON(debug_locks &&
  454. !!lockdep_is_held(&obj->base.dev->struct_mutex) !=
  455. !!(flags & I915_WAIT_LOCKED));
  456. #endif
  457. GEM_BUG_ON(timeout < 0);
  458. timeout = i915_gem_object_wait_reservation(obj->resv,
  459. flags, timeout,
  460. rps_client);
  461. return timeout < 0 ? timeout : 0;
  462. }
  463. static struct intel_rps_client *to_rps_client(struct drm_file *file)
  464. {
  465. struct drm_i915_file_private *fpriv = file->driver_priv;
  466. return &fpriv->rps_client;
  467. }
  468. static int
  469. i915_gem_phys_pwrite(struct drm_i915_gem_object *obj,
  470. struct drm_i915_gem_pwrite *args,
  471. struct drm_file *file)
  472. {
  473. void *vaddr = obj->phys_handle->vaddr + args->offset;
  474. char __user *user_data = u64_to_user_ptr(args->data_ptr);
  475. /* We manually control the domain here and pretend that it
  476. * remains coherent i.e. in the GTT domain, like shmem_pwrite.
  477. */
  478. intel_fb_obj_invalidate(obj, ORIGIN_CPU);
  479. if (copy_from_user(vaddr, user_data, args->size))
  480. return -EFAULT;
  481. drm_clflush_virt_range(vaddr, args->size);
  482. i915_gem_chipset_flush(to_i915(obj->base.dev));
  483. intel_fb_obj_flush(obj, ORIGIN_CPU);
  484. return 0;
  485. }
  486. void *i915_gem_object_alloc(struct drm_i915_private *dev_priv)
  487. {
  488. return kmem_cache_zalloc(dev_priv->objects, GFP_KERNEL);
  489. }
  490. void i915_gem_object_free(struct drm_i915_gem_object *obj)
  491. {
  492. struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  493. kmem_cache_free(dev_priv->objects, obj);
  494. }
  495. static int
  496. i915_gem_create(struct drm_file *file,
  497. struct drm_i915_private *dev_priv,
  498. uint64_t size,
  499. uint32_t *handle_p)
  500. {
  501. struct drm_i915_gem_object *obj;
  502. int ret;
  503. u32 handle;
  504. size = roundup(size, PAGE_SIZE);
  505. if (size == 0)
  506. return -EINVAL;
  507. /* Allocate the new object */
  508. obj = i915_gem_object_create(dev_priv, size);
  509. if (IS_ERR(obj))
  510. return PTR_ERR(obj);
  511. ret = drm_gem_handle_create(file, &obj->base, &handle);
  512. /* drop reference from allocate - handle holds it now */
  513. i915_gem_object_put(obj);
  514. if (ret)
  515. return ret;
  516. *handle_p = handle;
  517. return 0;
  518. }
  519. int
  520. i915_gem_dumb_create(struct drm_file *file,
  521. struct drm_device *dev,
  522. struct drm_mode_create_dumb *args)
  523. {
  524. /* have to work out size/pitch and return them */
  525. args->pitch = ALIGN(args->width * DIV_ROUND_UP(args->bpp, 8), 64);
  526. args->size = args->pitch * args->height;
  527. return i915_gem_create(file, to_i915(dev),
  528. args->size, &args->handle);
  529. }
  530. static bool gpu_write_needs_clflush(struct drm_i915_gem_object *obj)
  531. {
  532. return !(obj->cache_level == I915_CACHE_NONE ||
  533. obj->cache_level == I915_CACHE_WT);
  534. }
  535. /**
  536. * Creates a new mm object and returns a handle to it.
  537. * @dev: drm device pointer
  538. * @data: ioctl data blob
  539. * @file: drm file pointer
  540. */
  541. int
  542. i915_gem_create_ioctl(struct drm_device *dev, void *data,
  543. struct drm_file *file)
  544. {
  545. struct drm_i915_private *dev_priv = to_i915(dev);
  546. struct drm_i915_gem_create *args = data;
  547. i915_gem_flush_free_objects(dev_priv);
  548. return i915_gem_create(file, dev_priv,
  549. args->size, &args->handle);
  550. }
  551. static inline enum fb_op_origin
  552. fb_write_origin(struct drm_i915_gem_object *obj, unsigned int domain)
  553. {
  554. return (domain == I915_GEM_DOMAIN_GTT ?
  555. obj->frontbuffer_ggtt_origin : ORIGIN_CPU);
  556. }
  557. void i915_gem_flush_ggtt_writes(struct drm_i915_private *dev_priv)
  558. {
  559. /*
  560. * No actual flushing is required for the GTT write domain for reads
  561. * from the GTT domain. Writes to it "immediately" go to main memory
  562. * as far as we know, so there's no chipset flush. It also doesn't
  563. * land in the GPU render cache.
  564. *
  565. * However, we do have to enforce the order so that all writes through
  566. * the GTT land before any writes to the device, such as updates to
  567. * the GATT itself.
  568. *
  569. * We also have to wait a bit for the writes to land from the GTT.
  570. * An uncached read (i.e. mmio) seems to be ideal for the round-trip
  571. * timing. This issue has only been observed when switching quickly
  572. * between GTT writes and CPU reads from inside the kernel on recent hw,
  573. * and it appears to only affect discrete GTT blocks (i.e. on LLC
  574. * system agents we cannot reproduce this behaviour, until Cannonlake
  575. * that was!).
  576. */
  577. wmb();
  578. intel_runtime_pm_get(dev_priv);
  579. spin_lock_irq(&dev_priv->uncore.lock);
  580. POSTING_READ_FW(RING_HEAD(RENDER_RING_BASE));
  581. spin_unlock_irq(&dev_priv->uncore.lock);
  582. intel_runtime_pm_put(dev_priv);
  583. }
  584. static void
  585. flush_write_domain(struct drm_i915_gem_object *obj, unsigned int flush_domains)
  586. {
  587. struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  588. struct i915_vma *vma;
  589. if (!(obj->base.write_domain & flush_domains))
  590. return;
  591. switch (obj->base.write_domain) {
  592. case I915_GEM_DOMAIN_GTT:
  593. i915_gem_flush_ggtt_writes(dev_priv);
  594. intel_fb_obj_flush(obj,
  595. fb_write_origin(obj, I915_GEM_DOMAIN_GTT));
  596. for_each_ggtt_vma(vma, obj) {
  597. if (vma->iomap)
  598. continue;
  599. i915_vma_unset_ggtt_write(vma);
  600. }
  601. break;
  602. case I915_GEM_DOMAIN_CPU:
  603. i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC);
  604. break;
  605. case I915_GEM_DOMAIN_RENDER:
  606. if (gpu_write_needs_clflush(obj))
  607. obj->cache_dirty = true;
  608. break;
  609. }
  610. obj->base.write_domain = 0;
  611. }
  612. static inline int
  613. __copy_to_user_swizzled(char __user *cpu_vaddr,
  614. const char *gpu_vaddr, int gpu_offset,
  615. int length)
  616. {
  617. int ret, cpu_offset = 0;
  618. while (length > 0) {
  619. int cacheline_end = ALIGN(gpu_offset + 1, 64);
  620. int this_length = min(cacheline_end - gpu_offset, length);
  621. int swizzled_gpu_offset = gpu_offset ^ 64;
  622. ret = __copy_to_user(cpu_vaddr + cpu_offset,
  623. gpu_vaddr + swizzled_gpu_offset,
  624. this_length);
  625. if (ret)
  626. return ret + length;
  627. cpu_offset += this_length;
  628. gpu_offset += this_length;
  629. length -= this_length;
  630. }
  631. return 0;
  632. }
  633. static inline int
  634. __copy_from_user_swizzled(char *gpu_vaddr, int gpu_offset,
  635. const char __user *cpu_vaddr,
  636. int length)
  637. {
  638. int ret, cpu_offset = 0;
  639. while (length > 0) {
  640. int cacheline_end = ALIGN(gpu_offset + 1, 64);
  641. int this_length = min(cacheline_end - gpu_offset, length);
  642. int swizzled_gpu_offset = gpu_offset ^ 64;
  643. ret = __copy_from_user(gpu_vaddr + swizzled_gpu_offset,
  644. cpu_vaddr + cpu_offset,
  645. this_length);
  646. if (ret)
  647. return ret + length;
  648. cpu_offset += this_length;
  649. gpu_offset += this_length;
  650. length -= this_length;
  651. }
  652. return 0;
  653. }
  654. /*
  655. * Pins the specified object's pages and synchronizes the object with
  656. * GPU accesses. Sets needs_clflush to non-zero if the caller should
  657. * flush the object from the CPU cache.
  658. */
  659. int i915_gem_obj_prepare_shmem_read(struct drm_i915_gem_object *obj,
  660. unsigned int *needs_clflush)
  661. {
  662. int ret;
  663. lockdep_assert_held(&obj->base.dev->struct_mutex);
  664. *needs_clflush = 0;
  665. if (!i915_gem_object_has_struct_page(obj))
  666. return -ENODEV;
  667. ret = i915_gem_object_wait(obj,
  668. I915_WAIT_INTERRUPTIBLE |
  669. I915_WAIT_LOCKED,
  670. MAX_SCHEDULE_TIMEOUT,
  671. NULL);
  672. if (ret)
  673. return ret;
  674. ret = i915_gem_object_pin_pages(obj);
  675. if (ret)
  676. return ret;
  677. if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ ||
  678. !static_cpu_has(X86_FEATURE_CLFLUSH)) {
  679. ret = i915_gem_object_set_to_cpu_domain(obj, false);
  680. if (ret)
  681. goto err_unpin;
  682. else
  683. goto out;
  684. }
  685. flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
  686. /* If we're not in the cpu read domain, set ourself into the gtt
  687. * read domain and manually flush cachelines (if required). This
  688. * optimizes for the case when the gpu will dirty the data
  689. * anyway again before the next pread happens.
  690. */
  691. if (!obj->cache_dirty &&
  692. !(obj->base.read_domains & I915_GEM_DOMAIN_CPU))
  693. *needs_clflush = CLFLUSH_BEFORE;
  694. out:
  695. /* return with the pages pinned */
  696. return 0;
  697. err_unpin:
  698. i915_gem_object_unpin_pages(obj);
  699. return ret;
  700. }
  701. int i915_gem_obj_prepare_shmem_write(struct drm_i915_gem_object *obj,
  702. unsigned int *needs_clflush)
  703. {
  704. int ret;
  705. lockdep_assert_held(&obj->base.dev->struct_mutex);
  706. *needs_clflush = 0;
  707. if (!i915_gem_object_has_struct_page(obj))
  708. return -ENODEV;
  709. ret = i915_gem_object_wait(obj,
  710. I915_WAIT_INTERRUPTIBLE |
  711. I915_WAIT_LOCKED |
  712. I915_WAIT_ALL,
  713. MAX_SCHEDULE_TIMEOUT,
  714. NULL);
  715. if (ret)
  716. return ret;
  717. ret = i915_gem_object_pin_pages(obj);
  718. if (ret)
  719. return ret;
  720. if (obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE ||
  721. !static_cpu_has(X86_FEATURE_CLFLUSH)) {
  722. ret = i915_gem_object_set_to_cpu_domain(obj, true);
  723. if (ret)
  724. goto err_unpin;
  725. else
  726. goto out;
  727. }
  728. flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
  729. /* If we're not in the cpu write domain, set ourself into the
  730. * gtt write domain and manually flush cachelines (as required).
  731. * This optimizes for the case when the gpu will use the data
  732. * right away and we therefore have to clflush anyway.
  733. */
  734. if (!obj->cache_dirty) {
  735. *needs_clflush |= CLFLUSH_AFTER;
  736. /*
  737. * Same trick applies to invalidate partially written
  738. * cachelines read before writing.
  739. */
  740. if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU))
  741. *needs_clflush |= CLFLUSH_BEFORE;
  742. }
  743. out:
  744. intel_fb_obj_invalidate(obj, ORIGIN_CPU);
  745. obj->mm.dirty = true;
  746. /* return with the pages pinned */
  747. return 0;
  748. err_unpin:
  749. i915_gem_object_unpin_pages(obj);
  750. return ret;
  751. }
  752. static void
  753. shmem_clflush_swizzled_range(char *addr, unsigned long length,
  754. bool swizzled)
  755. {
  756. if (unlikely(swizzled)) {
  757. unsigned long start = (unsigned long) addr;
  758. unsigned long end = (unsigned long) addr + length;
  759. /* For swizzling simply ensure that we always flush both
  760. * channels. Lame, but simple and it works. Swizzled
  761. * pwrite/pread is far from a hotpath - current userspace
  762. * doesn't use it at all. */
  763. start = round_down(start, 128);
  764. end = round_up(end, 128);
  765. drm_clflush_virt_range((void *)start, end - start);
  766. } else {
  767. drm_clflush_virt_range(addr, length);
  768. }
  769. }
  770. /* Only difference to the fast-path function is that this can handle bit17
  771. * and uses non-atomic copy and kmap functions. */
  772. static int
  773. shmem_pread_slow(struct page *page, int offset, int length,
  774. char __user *user_data,
  775. bool page_do_bit17_swizzling, bool needs_clflush)
  776. {
  777. char *vaddr;
  778. int ret;
  779. vaddr = kmap(page);
  780. if (needs_clflush)
  781. shmem_clflush_swizzled_range(vaddr + offset, length,
  782. page_do_bit17_swizzling);
  783. if (page_do_bit17_swizzling)
  784. ret = __copy_to_user_swizzled(user_data, vaddr, offset, length);
  785. else
  786. ret = __copy_to_user(user_data, vaddr + offset, length);
  787. kunmap(page);
  788. return ret ? - EFAULT : 0;
  789. }
  790. static int
  791. shmem_pread(struct page *page, int offset, int length, char __user *user_data,
  792. bool page_do_bit17_swizzling, bool needs_clflush)
  793. {
  794. int ret;
  795. ret = -ENODEV;
  796. if (!page_do_bit17_swizzling) {
  797. char *vaddr = kmap_atomic(page);
  798. if (needs_clflush)
  799. drm_clflush_virt_range(vaddr + offset, length);
  800. ret = __copy_to_user_inatomic(user_data, vaddr + offset, length);
  801. kunmap_atomic(vaddr);
  802. }
  803. if (ret == 0)
  804. return 0;
  805. return shmem_pread_slow(page, offset, length, user_data,
  806. page_do_bit17_swizzling, needs_clflush);
  807. }
  808. static int
  809. i915_gem_shmem_pread(struct drm_i915_gem_object *obj,
  810. struct drm_i915_gem_pread *args)
  811. {
  812. char __user *user_data;
  813. u64 remain;
  814. unsigned int obj_do_bit17_swizzling;
  815. unsigned int needs_clflush;
  816. unsigned int idx, offset;
  817. int ret;
  818. obj_do_bit17_swizzling = 0;
  819. if (i915_gem_object_needs_bit17_swizzle(obj))
  820. obj_do_bit17_swizzling = BIT(17);
  821. ret = mutex_lock_interruptible(&obj->base.dev->struct_mutex);
  822. if (ret)
  823. return ret;
  824. ret = i915_gem_obj_prepare_shmem_read(obj, &needs_clflush);
  825. mutex_unlock(&obj->base.dev->struct_mutex);
  826. if (ret)
  827. return ret;
  828. remain = args->size;
  829. user_data = u64_to_user_ptr(args->data_ptr);
  830. offset = offset_in_page(args->offset);
  831. for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
  832. struct page *page = i915_gem_object_get_page(obj, idx);
  833. int length;
  834. length = remain;
  835. if (offset + length > PAGE_SIZE)
  836. length = PAGE_SIZE - offset;
  837. ret = shmem_pread(page, offset, length, user_data,
  838. page_to_phys(page) & obj_do_bit17_swizzling,
  839. needs_clflush);
  840. if (ret)
  841. break;
  842. remain -= length;
  843. user_data += length;
  844. offset = 0;
  845. }
  846. i915_gem_obj_finish_shmem_access(obj);
  847. return ret;
  848. }
  849. static inline bool
  850. gtt_user_read(struct io_mapping *mapping,
  851. loff_t base, int offset,
  852. char __user *user_data, int length)
  853. {
  854. void __iomem *vaddr;
  855. unsigned long unwritten;
  856. /* We can use the cpu mem copy function because this is X86. */
  857. vaddr = io_mapping_map_atomic_wc(mapping, base);
  858. unwritten = __copy_to_user_inatomic(user_data,
  859. (void __force *)vaddr + offset,
  860. length);
  861. io_mapping_unmap_atomic(vaddr);
  862. if (unwritten) {
  863. vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE);
  864. unwritten = copy_to_user(user_data,
  865. (void __force *)vaddr + offset,
  866. length);
  867. io_mapping_unmap(vaddr);
  868. }
  869. return unwritten;
  870. }
  871. static int
  872. i915_gem_gtt_pread(struct drm_i915_gem_object *obj,
  873. const struct drm_i915_gem_pread *args)
  874. {
  875. struct drm_i915_private *i915 = to_i915(obj->base.dev);
  876. struct i915_ggtt *ggtt = &i915->ggtt;
  877. struct drm_mm_node node;
  878. struct i915_vma *vma;
  879. void __user *user_data;
  880. u64 remain, offset;
  881. int ret;
  882. ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
  883. if (ret)
  884. return ret;
  885. intel_runtime_pm_get(i915);
  886. vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
  887. PIN_MAPPABLE |
  888. PIN_NONFAULT |
  889. PIN_NONBLOCK);
  890. if (!IS_ERR(vma)) {
  891. node.start = i915_ggtt_offset(vma);
  892. node.allocated = false;
  893. ret = i915_vma_put_fence(vma);
  894. if (ret) {
  895. i915_vma_unpin(vma);
  896. vma = ERR_PTR(ret);
  897. }
  898. }
  899. if (IS_ERR(vma)) {
  900. ret = insert_mappable_node(ggtt, &node, PAGE_SIZE);
  901. if (ret)
  902. goto out_unlock;
  903. GEM_BUG_ON(!node.allocated);
  904. }
  905. ret = i915_gem_object_set_to_gtt_domain(obj, false);
  906. if (ret)
  907. goto out_unpin;
  908. mutex_unlock(&i915->drm.struct_mutex);
  909. user_data = u64_to_user_ptr(args->data_ptr);
  910. remain = args->size;
  911. offset = args->offset;
  912. while (remain > 0) {
  913. /* Operation in this page
  914. *
  915. * page_base = page offset within aperture
  916. * page_offset = offset within page
  917. * page_length = bytes to copy for this page
  918. */
  919. u32 page_base = node.start;
  920. unsigned page_offset = offset_in_page(offset);
  921. unsigned page_length = PAGE_SIZE - page_offset;
  922. page_length = remain < page_length ? remain : page_length;
  923. if (node.allocated) {
  924. wmb();
  925. ggtt->base.insert_page(&ggtt->base,
  926. i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
  927. node.start, I915_CACHE_NONE, 0);
  928. wmb();
  929. } else {
  930. page_base += offset & PAGE_MASK;
  931. }
  932. if (gtt_user_read(&ggtt->iomap, page_base, page_offset,
  933. user_data, page_length)) {
  934. ret = -EFAULT;
  935. break;
  936. }
  937. remain -= page_length;
  938. user_data += page_length;
  939. offset += page_length;
  940. }
  941. mutex_lock(&i915->drm.struct_mutex);
  942. out_unpin:
  943. if (node.allocated) {
  944. wmb();
  945. ggtt->base.clear_range(&ggtt->base,
  946. node.start, node.size);
  947. remove_mappable_node(&node);
  948. } else {
  949. i915_vma_unpin(vma);
  950. }
  951. out_unlock:
  952. intel_runtime_pm_put(i915);
  953. mutex_unlock(&i915->drm.struct_mutex);
  954. return ret;
  955. }
  956. /**
  957. * Reads data from the object referenced by handle.
  958. * @dev: drm device pointer
  959. * @data: ioctl data blob
  960. * @file: drm file pointer
  961. *
  962. * On error, the contents of *data are undefined.
  963. */
  964. int
  965. i915_gem_pread_ioctl(struct drm_device *dev, void *data,
  966. struct drm_file *file)
  967. {
  968. struct drm_i915_gem_pread *args = data;
  969. struct drm_i915_gem_object *obj;
  970. int ret;
  971. if (args->size == 0)
  972. return 0;
  973. if (!access_ok(VERIFY_WRITE,
  974. u64_to_user_ptr(args->data_ptr),
  975. args->size))
  976. return -EFAULT;
  977. obj = i915_gem_object_lookup(file, args->handle);
  978. if (!obj)
  979. return -ENOENT;
  980. /* Bounds check source. */
  981. if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
  982. ret = -EINVAL;
  983. goto out;
  984. }
  985. trace_i915_gem_object_pread(obj, args->offset, args->size);
  986. ret = i915_gem_object_wait(obj,
  987. I915_WAIT_INTERRUPTIBLE,
  988. MAX_SCHEDULE_TIMEOUT,
  989. to_rps_client(file));
  990. if (ret)
  991. goto out;
  992. ret = i915_gem_object_pin_pages(obj);
  993. if (ret)
  994. goto out;
  995. ret = i915_gem_shmem_pread(obj, args);
  996. if (ret == -EFAULT || ret == -ENODEV)
  997. ret = i915_gem_gtt_pread(obj, args);
  998. i915_gem_object_unpin_pages(obj);
  999. out:
  1000. i915_gem_object_put(obj);
  1001. return ret;
  1002. }
  1003. /* This is the fast write path which cannot handle
  1004. * page faults in the source data
  1005. */
  1006. static inline bool
  1007. ggtt_write(struct io_mapping *mapping,
  1008. loff_t base, int offset,
  1009. char __user *user_data, int length)
  1010. {
  1011. void __iomem *vaddr;
  1012. unsigned long unwritten;
  1013. /* We can use the cpu mem copy function because this is X86. */
  1014. vaddr = io_mapping_map_atomic_wc(mapping, base);
  1015. unwritten = __copy_from_user_inatomic_nocache((void __force *)vaddr + offset,
  1016. user_data, length);
  1017. io_mapping_unmap_atomic(vaddr);
  1018. if (unwritten) {
  1019. vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE);
  1020. unwritten = copy_from_user((void __force *)vaddr + offset,
  1021. user_data, length);
  1022. io_mapping_unmap(vaddr);
  1023. }
  1024. return unwritten;
  1025. }
  1026. /**
  1027. * This is the fast pwrite path, where we copy the data directly from the
  1028. * user into the GTT, uncached.
  1029. * @obj: i915 GEM object
  1030. * @args: pwrite arguments structure
  1031. */
  1032. static int
  1033. i915_gem_gtt_pwrite_fast(struct drm_i915_gem_object *obj,
  1034. const struct drm_i915_gem_pwrite *args)
  1035. {
  1036. struct drm_i915_private *i915 = to_i915(obj->base.dev);
  1037. struct i915_ggtt *ggtt = &i915->ggtt;
  1038. struct drm_mm_node node;
  1039. struct i915_vma *vma;
  1040. u64 remain, offset;
  1041. void __user *user_data;
  1042. int ret;
  1043. ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
  1044. if (ret)
  1045. return ret;
  1046. if (i915_gem_object_has_struct_page(obj)) {
  1047. /*
  1048. * Avoid waking the device up if we can fallback, as
  1049. * waking/resuming is very slow (worst-case 10-100 ms
  1050. * depending on PCI sleeps and our own resume time).
  1051. * This easily dwarfs any performance advantage from
  1052. * using the cache bypass of indirect GGTT access.
  1053. */
  1054. if (!intel_runtime_pm_get_if_in_use(i915)) {
  1055. ret = -EFAULT;
  1056. goto out_unlock;
  1057. }
  1058. } else {
  1059. /* No backing pages, no fallback, we must force GGTT access */
  1060. intel_runtime_pm_get(i915);
  1061. }
  1062. vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
  1063. PIN_MAPPABLE |
  1064. PIN_NONFAULT |
  1065. PIN_NONBLOCK);
  1066. if (!IS_ERR(vma)) {
  1067. node.start = i915_ggtt_offset(vma);
  1068. node.allocated = false;
  1069. ret = i915_vma_put_fence(vma);
  1070. if (ret) {
  1071. i915_vma_unpin(vma);
  1072. vma = ERR_PTR(ret);
  1073. }
  1074. }
  1075. if (IS_ERR(vma)) {
  1076. ret = insert_mappable_node(ggtt, &node, PAGE_SIZE);
  1077. if (ret)
  1078. goto out_rpm;
  1079. GEM_BUG_ON(!node.allocated);
  1080. }
  1081. ret = i915_gem_object_set_to_gtt_domain(obj, true);
  1082. if (ret)
  1083. goto out_unpin;
  1084. mutex_unlock(&i915->drm.struct_mutex);
  1085. intel_fb_obj_invalidate(obj, ORIGIN_CPU);
  1086. user_data = u64_to_user_ptr(args->data_ptr);
  1087. offset = args->offset;
  1088. remain = args->size;
  1089. while (remain) {
  1090. /* Operation in this page
  1091. *
  1092. * page_base = page offset within aperture
  1093. * page_offset = offset within page
  1094. * page_length = bytes to copy for this page
  1095. */
  1096. u32 page_base = node.start;
  1097. unsigned int page_offset = offset_in_page(offset);
  1098. unsigned int page_length = PAGE_SIZE - page_offset;
  1099. page_length = remain < page_length ? remain : page_length;
  1100. if (node.allocated) {
  1101. wmb(); /* flush the write before we modify the GGTT */
  1102. ggtt->base.insert_page(&ggtt->base,
  1103. i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
  1104. node.start, I915_CACHE_NONE, 0);
  1105. wmb(); /* flush modifications to the GGTT (insert_page) */
  1106. } else {
  1107. page_base += offset & PAGE_MASK;
  1108. }
  1109. /* If we get a fault while copying data, then (presumably) our
  1110. * source page isn't available. Return the error and we'll
  1111. * retry in the slow path.
  1112. * If the object is non-shmem backed, we retry again with the
  1113. * path that handles page fault.
  1114. */
  1115. if (ggtt_write(&ggtt->iomap, page_base, page_offset,
  1116. user_data, page_length)) {
  1117. ret = -EFAULT;
  1118. break;
  1119. }
  1120. remain -= page_length;
  1121. user_data += page_length;
  1122. offset += page_length;
  1123. }
  1124. intel_fb_obj_flush(obj, ORIGIN_CPU);
  1125. mutex_lock(&i915->drm.struct_mutex);
  1126. out_unpin:
  1127. if (node.allocated) {
  1128. wmb();
  1129. ggtt->base.clear_range(&ggtt->base,
  1130. node.start, node.size);
  1131. remove_mappable_node(&node);
  1132. } else {
  1133. i915_vma_unpin(vma);
  1134. }
  1135. out_rpm:
  1136. intel_runtime_pm_put(i915);
  1137. out_unlock:
  1138. mutex_unlock(&i915->drm.struct_mutex);
  1139. return ret;
  1140. }
  1141. static int
  1142. shmem_pwrite_slow(struct page *page, int offset, int length,
  1143. char __user *user_data,
  1144. bool page_do_bit17_swizzling,
  1145. bool needs_clflush_before,
  1146. bool needs_clflush_after)
  1147. {
  1148. char *vaddr;
  1149. int ret;
  1150. vaddr = kmap(page);
  1151. if (unlikely(needs_clflush_before || page_do_bit17_swizzling))
  1152. shmem_clflush_swizzled_range(vaddr + offset, length,
  1153. page_do_bit17_swizzling);
  1154. if (page_do_bit17_swizzling)
  1155. ret = __copy_from_user_swizzled(vaddr, offset, user_data,
  1156. length);
  1157. else
  1158. ret = __copy_from_user(vaddr + offset, user_data, length);
  1159. if (needs_clflush_after)
  1160. shmem_clflush_swizzled_range(vaddr + offset, length,
  1161. page_do_bit17_swizzling);
  1162. kunmap(page);
  1163. return ret ? -EFAULT : 0;
  1164. }
  1165. /* Per-page copy function for the shmem pwrite fastpath.
  1166. * Flushes invalid cachelines before writing to the target if
  1167. * needs_clflush_before is set and flushes out any written cachelines after
  1168. * writing if needs_clflush is set.
  1169. */
  1170. static int
  1171. shmem_pwrite(struct page *page, int offset, int len, char __user *user_data,
  1172. bool page_do_bit17_swizzling,
  1173. bool needs_clflush_before,
  1174. bool needs_clflush_after)
  1175. {
  1176. int ret;
  1177. ret = -ENODEV;
  1178. if (!page_do_bit17_swizzling) {
  1179. char *vaddr = kmap_atomic(page);
  1180. if (needs_clflush_before)
  1181. drm_clflush_virt_range(vaddr + offset, len);
  1182. ret = __copy_from_user_inatomic(vaddr + offset, user_data, len);
  1183. if (needs_clflush_after)
  1184. drm_clflush_virt_range(vaddr + offset, len);
  1185. kunmap_atomic(vaddr);
  1186. }
  1187. if (ret == 0)
  1188. return ret;
  1189. return shmem_pwrite_slow(page, offset, len, user_data,
  1190. page_do_bit17_swizzling,
  1191. needs_clflush_before,
  1192. needs_clflush_after);
  1193. }
  1194. static int
  1195. i915_gem_shmem_pwrite(struct drm_i915_gem_object *obj,
  1196. const struct drm_i915_gem_pwrite *args)
  1197. {
  1198. struct drm_i915_private *i915 = to_i915(obj->base.dev);
  1199. void __user *user_data;
  1200. u64 remain;
  1201. unsigned int obj_do_bit17_swizzling;
  1202. unsigned int partial_cacheline_write;
  1203. unsigned int needs_clflush;
  1204. unsigned int offset, idx;
  1205. int ret;
  1206. ret = mutex_lock_interruptible(&i915->drm.struct_mutex);
  1207. if (ret)
  1208. return ret;
  1209. ret = i915_gem_obj_prepare_shmem_write(obj, &needs_clflush);
  1210. mutex_unlock(&i915->drm.struct_mutex);
  1211. if (ret)
  1212. return ret;
  1213. obj_do_bit17_swizzling = 0;
  1214. if (i915_gem_object_needs_bit17_swizzle(obj))
  1215. obj_do_bit17_swizzling = BIT(17);
  1216. /* If we don't overwrite a cacheline completely we need to be
  1217. * careful to have up-to-date data by first clflushing. Don't
  1218. * overcomplicate things and flush the entire patch.
  1219. */
  1220. partial_cacheline_write = 0;
  1221. if (needs_clflush & CLFLUSH_BEFORE)
  1222. partial_cacheline_write = boot_cpu_data.x86_clflush_size - 1;
  1223. user_data = u64_to_user_ptr(args->data_ptr);
  1224. remain = args->size;
  1225. offset = offset_in_page(args->offset);
  1226. for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
  1227. struct page *page = i915_gem_object_get_page(obj, idx);
  1228. int length;
  1229. length = remain;
  1230. if (offset + length > PAGE_SIZE)
  1231. length = PAGE_SIZE - offset;
  1232. ret = shmem_pwrite(page, offset, length, user_data,
  1233. page_to_phys(page) & obj_do_bit17_swizzling,
  1234. (offset | length) & partial_cacheline_write,
  1235. needs_clflush & CLFLUSH_AFTER);
  1236. if (ret)
  1237. break;
  1238. remain -= length;
  1239. user_data += length;
  1240. offset = 0;
  1241. }
  1242. intel_fb_obj_flush(obj, ORIGIN_CPU);
  1243. i915_gem_obj_finish_shmem_access(obj);
  1244. return ret;
  1245. }
  1246. /**
  1247. * Writes data to the object referenced by handle.
  1248. * @dev: drm device
  1249. * @data: ioctl data blob
  1250. * @file: drm file
  1251. *
  1252. * On error, the contents of the buffer that were to be modified are undefined.
  1253. */
  1254. int
  1255. i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
  1256. struct drm_file *file)
  1257. {
  1258. struct drm_i915_gem_pwrite *args = data;
  1259. struct drm_i915_gem_object *obj;
  1260. int ret;
  1261. if (args->size == 0)
  1262. return 0;
  1263. if (!access_ok(VERIFY_READ,
  1264. u64_to_user_ptr(args->data_ptr),
  1265. args->size))
  1266. return -EFAULT;
  1267. obj = i915_gem_object_lookup(file, args->handle);
  1268. if (!obj)
  1269. return -ENOENT;
  1270. /* Bounds check destination. */
  1271. if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
  1272. ret = -EINVAL;
  1273. goto err;
  1274. }
  1275. trace_i915_gem_object_pwrite(obj, args->offset, args->size);
  1276. ret = -ENODEV;
  1277. if (obj->ops->pwrite)
  1278. ret = obj->ops->pwrite(obj, args);
  1279. if (ret != -ENODEV)
  1280. goto err;
  1281. ret = i915_gem_object_wait(obj,
  1282. I915_WAIT_INTERRUPTIBLE |
  1283. I915_WAIT_ALL,
  1284. MAX_SCHEDULE_TIMEOUT,
  1285. to_rps_client(file));
  1286. if (ret)
  1287. goto err;
  1288. ret = i915_gem_object_pin_pages(obj);
  1289. if (ret)
  1290. goto err;
  1291. ret = -EFAULT;
  1292. /* We can only do the GTT pwrite on untiled buffers, as otherwise
  1293. * it would end up going through the fenced access, and we'll get
  1294. * different detiling behavior between reading and writing.
  1295. * pread/pwrite currently are reading and writing from the CPU
  1296. * perspective, requiring manual detiling by the client.
  1297. */
  1298. if (!i915_gem_object_has_struct_page(obj) ||
  1299. cpu_write_needs_clflush(obj))
  1300. /* Note that the gtt paths might fail with non-page-backed user
  1301. * pointers (e.g. gtt mappings when moving data between
  1302. * textures). Fallback to the shmem path in that case.
  1303. */
  1304. ret = i915_gem_gtt_pwrite_fast(obj, args);
  1305. if (ret == -EFAULT || ret == -ENOSPC) {
  1306. if (obj->phys_handle)
  1307. ret = i915_gem_phys_pwrite(obj, args, file);
  1308. else
  1309. ret = i915_gem_shmem_pwrite(obj, args);
  1310. }
  1311. i915_gem_object_unpin_pages(obj);
  1312. err:
  1313. i915_gem_object_put(obj);
  1314. return ret;
  1315. }
  1316. static void i915_gem_object_bump_inactive_ggtt(struct drm_i915_gem_object *obj)
  1317. {
  1318. struct drm_i915_private *i915;
  1319. struct list_head *list;
  1320. struct i915_vma *vma;
  1321. GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
  1322. for_each_ggtt_vma(vma, obj) {
  1323. if (i915_vma_is_active(vma))
  1324. continue;
  1325. if (!drm_mm_node_allocated(&vma->node))
  1326. continue;
  1327. list_move_tail(&vma->vm_link, &vma->vm->inactive_list);
  1328. }
  1329. i915 = to_i915(obj->base.dev);
  1330. spin_lock(&i915->mm.obj_lock);
  1331. list = obj->bind_count ? &i915->mm.bound_list : &i915->mm.unbound_list;
  1332. list_move_tail(&obj->mm.link, list);
  1333. spin_unlock(&i915->mm.obj_lock);
  1334. }
  1335. /**
  1336. * Called when user space prepares to use an object with the CPU, either
  1337. * through the mmap ioctl's mapping or a GTT mapping.
  1338. * @dev: drm device
  1339. * @data: ioctl data blob
  1340. * @file: drm file
  1341. */
  1342. int
  1343. i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
  1344. struct drm_file *file)
  1345. {
  1346. struct drm_i915_gem_set_domain *args = data;
  1347. struct drm_i915_gem_object *obj;
  1348. uint32_t read_domains = args->read_domains;
  1349. uint32_t write_domain = args->write_domain;
  1350. int err;
  1351. /* Only handle setting domains to types used by the CPU. */
  1352. if ((write_domain | read_domains) & I915_GEM_GPU_DOMAINS)
  1353. return -EINVAL;
  1354. /* Having something in the write domain implies it's in the read
  1355. * domain, and only that read domain. Enforce that in the request.
  1356. */
  1357. if (write_domain != 0 && read_domains != write_domain)
  1358. return -EINVAL;
  1359. obj = i915_gem_object_lookup(file, args->handle);
  1360. if (!obj)
  1361. return -ENOENT;
  1362. /* Try to flush the object off the GPU without holding the lock.
  1363. * We will repeat the flush holding the lock in the normal manner
  1364. * to catch cases where we are gazumped.
  1365. */
  1366. err = i915_gem_object_wait(obj,
  1367. I915_WAIT_INTERRUPTIBLE |
  1368. (write_domain ? I915_WAIT_ALL : 0),
  1369. MAX_SCHEDULE_TIMEOUT,
  1370. to_rps_client(file));
  1371. if (err)
  1372. goto out;
  1373. /*
  1374. * Proxy objects do not control access to the backing storage, ergo
  1375. * they cannot be used as a means to manipulate the cache domain
  1376. * tracking for that backing storage. The proxy object is always
  1377. * considered to be outside of any cache domain.
  1378. */
  1379. if (i915_gem_object_is_proxy(obj)) {
  1380. err = -ENXIO;
  1381. goto out;
  1382. }
  1383. /*
  1384. * Flush and acquire obj->pages so that we are coherent through
  1385. * direct access in memory with previous cached writes through
  1386. * shmemfs and that our cache domain tracking remains valid.
  1387. * For example, if the obj->filp was moved to swap without us
  1388. * being notified and releasing the pages, we would mistakenly
  1389. * continue to assume that the obj remained out of the CPU cached
  1390. * domain.
  1391. */
  1392. err = i915_gem_object_pin_pages(obj);
  1393. if (err)
  1394. goto out;
  1395. err = i915_mutex_lock_interruptible(dev);
  1396. if (err)
  1397. goto out_unpin;
  1398. if (read_domains & I915_GEM_DOMAIN_WC)
  1399. err = i915_gem_object_set_to_wc_domain(obj, write_domain);
  1400. else if (read_domains & I915_GEM_DOMAIN_GTT)
  1401. err = i915_gem_object_set_to_gtt_domain(obj, write_domain);
  1402. else
  1403. err = i915_gem_object_set_to_cpu_domain(obj, write_domain);
  1404. /* And bump the LRU for this access */
  1405. i915_gem_object_bump_inactive_ggtt(obj);
  1406. mutex_unlock(&dev->struct_mutex);
  1407. if (write_domain != 0)
  1408. intel_fb_obj_invalidate(obj,
  1409. fb_write_origin(obj, write_domain));
  1410. out_unpin:
  1411. i915_gem_object_unpin_pages(obj);
  1412. out:
  1413. i915_gem_object_put(obj);
  1414. return err;
  1415. }
  1416. /**
  1417. * Called when user space has done writes to this buffer
  1418. * @dev: drm device
  1419. * @data: ioctl data blob
  1420. * @file: drm file
  1421. */
  1422. int
  1423. i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
  1424. struct drm_file *file)
  1425. {
  1426. struct drm_i915_gem_sw_finish *args = data;
  1427. struct drm_i915_gem_object *obj;
  1428. obj = i915_gem_object_lookup(file, args->handle);
  1429. if (!obj)
  1430. return -ENOENT;
  1431. /*
  1432. * Proxy objects are barred from CPU access, so there is no
  1433. * need to ban sw_finish as it is a nop.
  1434. */
  1435. /* Pinned buffers may be scanout, so flush the cache */
  1436. i915_gem_object_flush_if_display(obj);
  1437. i915_gem_object_put(obj);
  1438. return 0;
  1439. }
  1440. /**
  1441. * i915_gem_mmap_ioctl - Maps the contents of an object, returning the address
  1442. * it is mapped to.
  1443. * @dev: drm device
  1444. * @data: ioctl data blob
  1445. * @file: drm file
  1446. *
  1447. * While the mapping holds a reference on the contents of the object, it doesn't
  1448. * imply a ref on the object itself.
  1449. *
  1450. * IMPORTANT:
  1451. *
  1452. * DRM driver writers who look a this function as an example for how to do GEM
  1453. * mmap support, please don't implement mmap support like here. The modern way
  1454. * to implement DRM mmap support is with an mmap offset ioctl (like
  1455. * i915_gem_mmap_gtt) and then using the mmap syscall on the DRM fd directly.
  1456. * That way debug tooling like valgrind will understand what's going on, hiding
  1457. * the mmap call in a driver private ioctl will break that. The i915 driver only
  1458. * does cpu mmaps this way because we didn't know better.
  1459. */
  1460. int
  1461. i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
  1462. struct drm_file *file)
  1463. {
  1464. struct drm_i915_gem_mmap *args = data;
  1465. struct drm_i915_gem_object *obj;
  1466. unsigned long addr;
  1467. if (args->flags & ~(I915_MMAP_WC))
  1468. return -EINVAL;
  1469. if (args->flags & I915_MMAP_WC && !boot_cpu_has(X86_FEATURE_PAT))
  1470. return -ENODEV;
  1471. obj = i915_gem_object_lookup(file, args->handle);
  1472. if (!obj)
  1473. return -ENOENT;
  1474. /* prime objects have no backing filp to GEM mmap
  1475. * pages from.
  1476. */
  1477. if (!obj->base.filp) {
  1478. i915_gem_object_put(obj);
  1479. return -ENXIO;
  1480. }
  1481. addr = vm_mmap(obj->base.filp, 0, args->size,
  1482. PROT_READ | PROT_WRITE, MAP_SHARED,
  1483. args->offset);
  1484. if (args->flags & I915_MMAP_WC) {
  1485. struct mm_struct *mm = current->mm;
  1486. struct vm_area_struct *vma;
  1487. if (down_write_killable(&mm->mmap_sem)) {
  1488. i915_gem_object_put(obj);
  1489. return -EINTR;
  1490. }
  1491. vma = find_vma(mm, addr);
  1492. if (vma)
  1493. vma->vm_page_prot =
  1494. pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
  1495. else
  1496. addr = -ENOMEM;
  1497. up_write(&mm->mmap_sem);
  1498. /* This may race, but that's ok, it only gets set */
  1499. WRITE_ONCE(obj->frontbuffer_ggtt_origin, ORIGIN_CPU);
  1500. }
  1501. i915_gem_object_put(obj);
  1502. if (IS_ERR((void *)addr))
  1503. return addr;
  1504. args->addr_ptr = (uint64_t) addr;
  1505. return 0;
  1506. }
  1507. static unsigned int tile_row_pages(struct drm_i915_gem_object *obj)
  1508. {
  1509. return i915_gem_object_get_tile_row_size(obj) >> PAGE_SHIFT;
  1510. }
  1511. /**
  1512. * i915_gem_mmap_gtt_version - report the current feature set for GTT mmaps
  1513. *
  1514. * A history of the GTT mmap interface:
  1515. *
  1516. * 0 - Everything had to fit into the GTT. Both parties of a memcpy had to
  1517. * aligned and suitable for fencing, and still fit into the available
  1518. * mappable space left by the pinned display objects. A classic problem
  1519. * we called the page-fault-of-doom where we would ping-pong between
  1520. * two objects that could not fit inside the GTT and so the memcpy
  1521. * would page one object in at the expense of the other between every
  1522. * single byte.
  1523. *
  1524. * 1 - Objects can be any size, and have any compatible fencing (X Y, or none
  1525. * as set via i915_gem_set_tiling() [DRM_I915_GEM_SET_TILING]). If the
  1526. * object is too large for the available space (or simply too large
  1527. * for the mappable aperture!), a view is created instead and faulted
  1528. * into userspace. (This view is aligned and sized appropriately for
  1529. * fenced access.)
  1530. *
  1531. * 2 - Recognise WC as a separate cache domain so that we can flush the
  1532. * delayed writes via GTT before performing direct access via WC.
  1533. *
  1534. * Restrictions:
  1535. *
  1536. * * snoopable objects cannot be accessed via the GTT. It can cause machine
  1537. * hangs on some architectures, corruption on others. An attempt to service
  1538. * a GTT page fault from a snoopable object will generate a SIGBUS.
  1539. *
  1540. * * the object must be able to fit into RAM (physical memory, though no
  1541. * limited to the mappable aperture).
  1542. *
  1543. *
  1544. * Caveats:
  1545. *
  1546. * * a new GTT page fault will synchronize rendering from the GPU and flush
  1547. * all data to system memory. Subsequent access will not be synchronized.
  1548. *
  1549. * * all mappings are revoked on runtime device suspend.
  1550. *
  1551. * * there are only 8, 16 or 32 fence registers to share between all users
  1552. * (older machines require fence register for display and blitter access
  1553. * as well). Contention of the fence registers will cause the previous users
  1554. * to be unmapped and any new access will generate new page faults.
  1555. *
  1556. * * running out of memory while servicing a fault may generate a SIGBUS,
  1557. * rather than the expected SIGSEGV.
  1558. */
  1559. int i915_gem_mmap_gtt_version(void)
  1560. {
  1561. return 2;
  1562. }
  1563. static inline struct i915_ggtt_view
  1564. compute_partial_view(struct drm_i915_gem_object *obj,
  1565. pgoff_t page_offset,
  1566. unsigned int chunk)
  1567. {
  1568. struct i915_ggtt_view view;
  1569. if (i915_gem_object_is_tiled(obj))
  1570. chunk = roundup(chunk, tile_row_pages(obj));
  1571. view.type = I915_GGTT_VIEW_PARTIAL;
  1572. view.partial.offset = rounddown(page_offset, chunk);
  1573. view.partial.size =
  1574. min_t(unsigned int, chunk,
  1575. (obj->base.size >> PAGE_SHIFT) - view.partial.offset);
  1576. /* If the partial covers the entire object, just create a normal VMA. */
  1577. if (chunk >= obj->base.size >> PAGE_SHIFT)
  1578. view.type = I915_GGTT_VIEW_NORMAL;
  1579. return view;
  1580. }
  1581. /**
  1582. * i915_gem_fault - fault a page into the GTT
  1583. * @vmf: fault info
  1584. *
  1585. * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
  1586. * from userspace. The fault handler takes care of binding the object to
  1587. * the GTT (if needed), allocating and programming a fence register (again,
  1588. * only if needed based on whether the old reg is still valid or the object
  1589. * is tiled) and inserting a new PTE into the faulting process.
  1590. *
  1591. * Note that the faulting process may involve evicting existing objects
  1592. * from the GTT and/or fence registers to make room. So performance may
  1593. * suffer if the GTT working set is large or there are few fence registers
  1594. * left.
  1595. *
  1596. * The current feature set supported by i915_gem_fault() and thus GTT mmaps
  1597. * is exposed via I915_PARAM_MMAP_GTT_VERSION (see i915_gem_mmap_gtt_version).
  1598. */
  1599. int i915_gem_fault(struct vm_fault *vmf)
  1600. {
  1601. #define MIN_CHUNK_PAGES ((1 << 20) >> PAGE_SHIFT) /* 1 MiB */
  1602. struct vm_area_struct *area = vmf->vma;
  1603. struct drm_i915_gem_object *obj = to_intel_bo(area->vm_private_data);
  1604. struct drm_device *dev = obj->base.dev;
  1605. struct drm_i915_private *dev_priv = to_i915(dev);
  1606. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  1607. bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
  1608. struct i915_vma *vma;
  1609. pgoff_t page_offset;
  1610. unsigned int flags;
  1611. int ret;
  1612. /* We don't use vmf->pgoff since that has the fake offset */
  1613. page_offset = (vmf->address - area->vm_start) >> PAGE_SHIFT;
  1614. trace_i915_gem_object_fault(obj, page_offset, true, write);
  1615. /* Try to flush the object off the GPU first without holding the lock.
  1616. * Upon acquiring the lock, we will perform our sanity checks and then
  1617. * repeat the flush holding the lock in the normal manner to catch cases
  1618. * where we are gazumped.
  1619. */
  1620. ret = i915_gem_object_wait(obj,
  1621. I915_WAIT_INTERRUPTIBLE,
  1622. MAX_SCHEDULE_TIMEOUT,
  1623. NULL);
  1624. if (ret)
  1625. goto err;
  1626. ret = i915_gem_object_pin_pages(obj);
  1627. if (ret)
  1628. goto err;
  1629. intel_runtime_pm_get(dev_priv);
  1630. ret = i915_mutex_lock_interruptible(dev);
  1631. if (ret)
  1632. goto err_rpm;
  1633. /* Access to snoopable pages through the GTT is incoherent. */
  1634. if (obj->cache_level != I915_CACHE_NONE && !HAS_LLC(dev_priv)) {
  1635. ret = -EFAULT;
  1636. goto err_unlock;
  1637. }
  1638. /* If the object is smaller than a couple of partial vma, it is
  1639. * not worth only creating a single partial vma - we may as well
  1640. * clear enough space for the full object.
  1641. */
  1642. flags = PIN_MAPPABLE;
  1643. if (obj->base.size > 2 * MIN_CHUNK_PAGES << PAGE_SHIFT)
  1644. flags |= PIN_NONBLOCK | PIN_NONFAULT;
  1645. /* Now pin it into the GTT as needed */
  1646. vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, flags);
  1647. if (IS_ERR(vma)) {
  1648. /* Use a partial view if it is bigger than available space */
  1649. struct i915_ggtt_view view =
  1650. compute_partial_view(obj, page_offset, MIN_CHUNK_PAGES);
  1651. /* Userspace is now writing through an untracked VMA, abandon
  1652. * all hope that the hardware is able to track future writes.
  1653. */
  1654. obj->frontbuffer_ggtt_origin = ORIGIN_CPU;
  1655. vma = i915_gem_object_ggtt_pin(obj, &view, 0, 0, PIN_MAPPABLE);
  1656. }
  1657. if (IS_ERR(vma)) {
  1658. ret = PTR_ERR(vma);
  1659. goto err_unlock;
  1660. }
  1661. ret = i915_gem_object_set_to_gtt_domain(obj, write);
  1662. if (ret)
  1663. goto err_unpin;
  1664. ret = i915_vma_pin_fence(vma);
  1665. if (ret)
  1666. goto err_unpin;
  1667. /* Finally, remap it using the new GTT offset */
  1668. ret = remap_io_mapping(area,
  1669. area->vm_start + (vma->ggtt_view.partial.offset << PAGE_SHIFT),
  1670. (ggtt->gmadr.start + vma->node.start) >> PAGE_SHIFT,
  1671. min_t(u64, vma->size, area->vm_end - area->vm_start),
  1672. &ggtt->iomap);
  1673. if (ret)
  1674. goto err_fence;
  1675. /* Mark as being mmapped into userspace for later revocation */
  1676. assert_rpm_wakelock_held(dev_priv);
  1677. if (!i915_vma_set_userfault(vma) && !obj->userfault_count++)
  1678. list_add(&obj->userfault_link, &dev_priv->mm.userfault_list);
  1679. GEM_BUG_ON(!obj->userfault_count);
  1680. i915_vma_set_ggtt_write(vma);
  1681. err_fence:
  1682. i915_vma_unpin_fence(vma);
  1683. err_unpin:
  1684. __i915_vma_unpin(vma);
  1685. err_unlock:
  1686. mutex_unlock(&dev->struct_mutex);
  1687. err_rpm:
  1688. intel_runtime_pm_put(dev_priv);
  1689. i915_gem_object_unpin_pages(obj);
  1690. err:
  1691. switch (ret) {
  1692. case -EIO:
  1693. /*
  1694. * We eat errors when the gpu is terminally wedged to avoid
  1695. * userspace unduly crashing (gl has no provisions for mmaps to
  1696. * fail). But any other -EIO isn't ours (e.g. swap in failure)
  1697. * and so needs to be reported.
  1698. */
  1699. if (!i915_terminally_wedged(&dev_priv->gpu_error)) {
  1700. ret = VM_FAULT_SIGBUS;
  1701. break;
  1702. }
  1703. case -EAGAIN:
  1704. /*
  1705. * EAGAIN means the gpu is hung and we'll wait for the error
  1706. * handler to reset everything when re-faulting in
  1707. * i915_mutex_lock_interruptible.
  1708. */
  1709. case 0:
  1710. case -ERESTARTSYS:
  1711. case -EINTR:
  1712. case -EBUSY:
  1713. /*
  1714. * EBUSY is ok: this just means that another thread
  1715. * already did the job.
  1716. */
  1717. ret = VM_FAULT_NOPAGE;
  1718. break;
  1719. case -ENOMEM:
  1720. ret = VM_FAULT_OOM;
  1721. break;
  1722. case -ENOSPC:
  1723. case -EFAULT:
  1724. ret = VM_FAULT_SIGBUS;
  1725. break;
  1726. default:
  1727. WARN_ONCE(ret, "unhandled error in i915_gem_fault: %i\n", ret);
  1728. ret = VM_FAULT_SIGBUS;
  1729. break;
  1730. }
  1731. return ret;
  1732. }
  1733. static void __i915_gem_object_release_mmap(struct drm_i915_gem_object *obj)
  1734. {
  1735. struct i915_vma *vma;
  1736. GEM_BUG_ON(!obj->userfault_count);
  1737. obj->userfault_count = 0;
  1738. list_del(&obj->userfault_link);
  1739. drm_vma_node_unmap(&obj->base.vma_node,
  1740. obj->base.dev->anon_inode->i_mapping);
  1741. for_each_ggtt_vma(vma, obj)
  1742. i915_vma_unset_userfault(vma);
  1743. }
  1744. /**
  1745. * i915_gem_release_mmap - remove physical page mappings
  1746. * @obj: obj in question
  1747. *
  1748. * Preserve the reservation of the mmapping with the DRM core code, but
  1749. * relinquish ownership of the pages back to the system.
  1750. *
  1751. * It is vital that we remove the page mapping if we have mapped a tiled
  1752. * object through the GTT and then lose the fence register due to
  1753. * resource pressure. Similarly if the object has been moved out of the
  1754. * aperture, than pages mapped into userspace must be revoked. Removing the
  1755. * mapping will then trigger a page fault on the next user access, allowing
  1756. * fixup by i915_gem_fault().
  1757. */
  1758. void
  1759. i915_gem_release_mmap(struct drm_i915_gem_object *obj)
  1760. {
  1761. struct drm_i915_private *i915 = to_i915(obj->base.dev);
  1762. /* Serialisation between user GTT access and our code depends upon
  1763. * revoking the CPU's PTE whilst the mutex is held. The next user
  1764. * pagefault then has to wait until we release the mutex.
  1765. *
  1766. * Note that RPM complicates somewhat by adding an additional
  1767. * requirement that operations to the GGTT be made holding the RPM
  1768. * wakeref.
  1769. */
  1770. lockdep_assert_held(&i915->drm.struct_mutex);
  1771. intel_runtime_pm_get(i915);
  1772. if (!obj->userfault_count)
  1773. goto out;
  1774. __i915_gem_object_release_mmap(obj);
  1775. /* Ensure that the CPU's PTE are revoked and there are not outstanding
  1776. * memory transactions from userspace before we return. The TLB
  1777. * flushing implied above by changing the PTE above *should* be
  1778. * sufficient, an extra barrier here just provides us with a bit
  1779. * of paranoid documentation about our requirement to serialise
  1780. * memory writes before touching registers / GSM.
  1781. */
  1782. wmb();
  1783. out:
  1784. intel_runtime_pm_put(i915);
  1785. }
  1786. void i915_gem_runtime_suspend(struct drm_i915_private *dev_priv)
  1787. {
  1788. struct drm_i915_gem_object *obj, *on;
  1789. int i;
  1790. /*
  1791. * Only called during RPM suspend. All users of the userfault_list
  1792. * must be holding an RPM wakeref to ensure that this can not
  1793. * run concurrently with themselves (and use the struct_mutex for
  1794. * protection between themselves).
  1795. */
  1796. list_for_each_entry_safe(obj, on,
  1797. &dev_priv->mm.userfault_list, userfault_link)
  1798. __i915_gem_object_release_mmap(obj);
  1799. /* The fence will be lost when the device powers down. If any were
  1800. * in use by hardware (i.e. they are pinned), we should not be powering
  1801. * down! All other fences will be reacquired by the user upon waking.
  1802. */
  1803. for (i = 0; i < dev_priv->num_fence_regs; i++) {
  1804. struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i];
  1805. /* Ideally we want to assert that the fence register is not
  1806. * live at this point (i.e. that no piece of code will be
  1807. * trying to write through fence + GTT, as that both violates
  1808. * our tracking of activity and associated locking/barriers,
  1809. * but also is illegal given that the hw is powered down).
  1810. *
  1811. * Previously we used reg->pin_count as a "liveness" indicator.
  1812. * That is not sufficient, and we need a more fine-grained
  1813. * tool if we want to have a sanity check here.
  1814. */
  1815. if (!reg->vma)
  1816. continue;
  1817. GEM_BUG_ON(i915_vma_has_userfault(reg->vma));
  1818. reg->dirty = true;
  1819. }
  1820. }
  1821. static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj)
  1822. {
  1823. struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  1824. int err;
  1825. err = drm_gem_create_mmap_offset(&obj->base);
  1826. if (likely(!err))
  1827. return 0;
  1828. /* Attempt to reap some mmap space from dead objects */
  1829. do {
  1830. err = i915_gem_wait_for_idle(dev_priv, I915_WAIT_INTERRUPTIBLE);
  1831. if (err)
  1832. break;
  1833. i915_gem_drain_freed_objects(dev_priv);
  1834. err = drm_gem_create_mmap_offset(&obj->base);
  1835. if (!err)
  1836. break;
  1837. } while (flush_delayed_work(&dev_priv->gt.retire_work));
  1838. return err;
  1839. }
  1840. static void i915_gem_object_free_mmap_offset(struct drm_i915_gem_object *obj)
  1841. {
  1842. drm_gem_free_mmap_offset(&obj->base);
  1843. }
  1844. int
  1845. i915_gem_mmap_gtt(struct drm_file *file,
  1846. struct drm_device *dev,
  1847. uint32_t handle,
  1848. uint64_t *offset)
  1849. {
  1850. struct drm_i915_gem_object *obj;
  1851. int ret;
  1852. obj = i915_gem_object_lookup(file, handle);
  1853. if (!obj)
  1854. return -ENOENT;
  1855. ret = i915_gem_object_create_mmap_offset(obj);
  1856. if (ret == 0)
  1857. *offset = drm_vma_node_offset_addr(&obj->base.vma_node);
  1858. i915_gem_object_put(obj);
  1859. return ret;
  1860. }
  1861. /**
  1862. * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
  1863. * @dev: DRM device
  1864. * @data: GTT mapping ioctl data
  1865. * @file: GEM object info
  1866. *
  1867. * Simply returns the fake offset to userspace so it can mmap it.
  1868. * The mmap call will end up in drm_gem_mmap(), which will set things
  1869. * up so we can get faults in the handler above.
  1870. *
  1871. * The fault handler will take care of binding the object into the GTT
  1872. * (since it may have been evicted to make room for something), allocating
  1873. * a fence register, and mapping the appropriate aperture address into
  1874. * userspace.
  1875. */
  1876. int
  1877. i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
  1878. struct drm_file *file)
  1879. {
  1880. struct drm_i915_gem_mmap_gtt *args = data;
  1881. return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset);
  1882. }
  1883. /* Immediately discard the backing storage */
  1884. static void
  1885. i915_gem_object_truncate(struct drm_i915_gem_object *obj)
  1886. {
  1887. i915_gem_object_free_mmap_offset(obj);
  1888. if (obj->base.filp == NULL)
  1889. return;
  1890. /* Our goal here is to return as much of the memory as
  1891. * is possible back to the system as we are called from OOM.
  1892. * To do this we must instruct the shmfs to drop all of its
  1893. * backing pages, *now*.
  1894. */
  1895. shmem_truncate_range(file_inode(obj->base.filp), 0, (loff_t)-1);
  1896. obj->mm.madv = __I915_MADV_PURGED;
  1897. obj->mm.pages = ERR_PTR(-EFAULT);
  1898. }
  1899. /* Try to discard unwanted pages */
  1900. void __i915_gem_object_invalidate(struct drm_i915_gem_object *obj)
  1901. {
  1902. struct address_space *mapping;
  1903. lockdep_assert_held(&obj->mm.lock);
  1904. GEM_BUG_ON(i915_gem_object_has_pages(obj));
  1905. switch (obj->mm.madv) {
  1906. case I915_MADV_DONTNEED:
  1907. i915_gem_object_truncate(obj);
  1908. case __I915_MADV_PURGED:
  1909. return;
  1910. }
  1911. if (obj->base.filp == NULL)
  1912. return;
  1913. mapping = obj->base.filp->f_mapping,
  1914. invalidate_mapping_pages(mapping, 0, (loff_t)-1);
  1915. }
  1916. static void
  1917. i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj,
  1918. struct sg_table *pages)
  1919. {
  1920. struct sgt_iter sgt_iter;
  1921. struct page *page;
  1922. __i915_gem_object_release_shmem(obj, pages, true);
  1923. i915_gem_gtt_finish_pages(obj, pages);
  1924. if (i915_gem_object_needs_bit17_swizzle(obj))
  1925. i915_gem_object_save_bit_17_swizzle(obj, pages);
  1926. for_each_sgt_page(page, sgt_iter, pages) {
  1927. if (obj->mm.dirty)
  1928. set_page_dirty(page);
  1929. if (obj->mm.madv == I915_MADV_WILLNEED)
  1930. mark_page_accessed(page);
  1931. put_page(page);
  1932. }
  1933. obj->mm.dirty = false;
  1934. sg_free_table(pages);
  1935. kfree(pages);
  1936. }
  1937. static void __i915_gem_object_reset_page_iter(struct drm_i915_gem_object *obj)
  1938. {
  1939. struct radix_tree_iter iter;
  1940. void __rcu **slot;
  1941. rcu_read_lock();
  1942. radix_tree_for_each_slot(slot, &obj->mm.get_page.radix, &iter, 0)
  1943. radix_tree_delete(&obj->mm.get_page.radix, iter.index);
  1944. rcu_read_unlock();
  1945. }
  1946. void __i915_gem_object_put_pages(struct drm_i915_gem_object *obj,
  1947. enum i915_mm_subclass subclass)
  1948. {
  1949. struct drm_i915_private *i915 = to_i915(obj->base.dev);
  1950. struct sg_table *pages;
  1951. if (i915_gem_object_has_pinned_pages(obj))
  1952. return;
  1953. GEM_BUG_ON(obj->bind_count);
  1954. if (!i915_gem_object_has_pages(obj))
  1955. return;
  1956. /* May be called by shrinker from within get_pages() (on another bo) */
  1957. mutex_lock_nested(&obj->mm.lock, subclass);
  1958. if (unlikely(atomic_read(&obj->mm.pages_pin_count)))
  1959. goto unlock;
  1960. /* ->put_pages might need to allocate memory for the bit17 swizzle
  1961. * array, hence protect them from being reaped by removing them from gtt
  1962. * lists early. */
  1963. pages = fetch_and_zero(&obj->mm.pages);
  1964. GEM_BUG_ON(!pages);
  1965. spin_lock(&i915->mm.obj_lock);
  1966. list_del(&obj->mm.link);
  1967. spin_unlock(&i915->mm.obj_lock);
  1968. if (obj->mm.mapping) {
  1969. void *ptr;
  1970. ptr = page_mask_bits(obj->mm.mapping);
  1971. if (is_vmalloc_addr(ptr))
  1972. vunmap(ptr);
  1973. else
  1974. kunmap(kmap_to_page(ptr));
  1975. obj->mm.mapping = NULL;
  1976. }
  1977. __i915_gem_object_reset_page_iter(obj);
  1978. if (!IS_ERR(pages))
  1979. obj->ops->put_pages(obj, pages);
  1980. obj->mm.page_sizes.phys = obj->mm.page_sizes.sg = 0;
  1981. unlock:
  1982. mutex_unlock(&obj->mm.lock);
  1983. }
  1984. static bool i915_sg_trim(struct sg_table *orig_st)
  1985. {
  1986. struct sg_table new_st;
  1987. struct scatterlist *sg, *new_sg;
  1988. unsigned int i;
  1989. if (orig_st->nents == orig_st->orig_nents)
  1990. return false;
  1991. if (sg_alloc_table(&new_st, orig_st->nents, GFP_KERNEL | __GFP_NOWARN))
  1992. return false;
  1993. new_sg = new_st.sgl;
  1994. for_each_sg(orig_st->sgl, sg, orig_st->nents, i) {
  1995. sg_set_page(new_sg, sg_page(sg), sg->length, 0);
  1996. /* called before being DMA mapped, no need to copy sg->dma_* */
  1997. new_sg = sg_next(new_sg);
  1998. }
  1999. GEM_BUG_ON(new_sg); /* Should walk exactly nents and hit the end */
  2000. sg_free_table(orig_st);
  2001. *orig_st = new_st;
  2002. return true;
  2003. }
  2004. static int i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
  2005. {
  2006. struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  2007. const unsigned long page_count = obj->base.size / PAGE_SIZE;
  2008. unsigned long i;
  2009. struct address_space *mapping;
  2010. struct sg_table *st;
  2011. struct scatterlist *sg;
  2012. struct sgt_iter sgt_iter;
  2013. struct page *page;
  2014. unsigned long last_pfn = 0; /* suppress gcc warning */
  2015. unsigned int max_segment = i915_sg_segment_size();
  2016. unsigned int sg_page_sizes;
  2017. gfp_t noreclaim;
  2018. int ret;
  2019. /* Assert that the object is not currently in any GPU domain. As it
  2020. * wasn't in the GTT, there shouldn't be any way it could have been in
  2021. * a GPU cache
  2022. */
  2023. GEM_BUG_ON(obj->base.read_domains & I915_GEM_GPU_DOMAINS);
  2024. GEM_BUG_ON(obj->base.write_domain & I915_GEM_GPU_DOMAINS);
  2025. st = kmalloc(sizeof(*st), GFP_KERNEL);
  2026. if (st == NULL)
  2027. return -ENOMEM;
  2028. rebuild_st:
  2029. if (sg_alloc_table(st, page_count, GFP_KERNEL)) {
  2030. kfree(st);
  2031. return -ENOMEM;
  2032. }
  2033. /* Get the list of pages out of our struct file. They'll be pinned
  2034. * at this point until we release them.
  2035. *
  2036. * Fail silently without starting the shrinker
  2037. */
  2038. mapping = obj->base.filp->f_mapping;
  2039. noreclaim = mapping_gfp_constraint(mapping, ~__GFP_RECLAIM);
  2040. noreclaim |= __GFP_NORETRY | __GFP_NOWARN;
  2041. sg = st->sgl;
  2042. st->nents = 0;
  2043. sg_page_sizes = 0;
  2044. for (i = 0; i < page_count; i++) {
  2045. const unsigned int shrink[] = {
  2046. I915_SHRINK_BOUND | I915_SHRINK_UNBOUND | I915_SHRINK_PURGEABLE,
  2047. 0,
  2048. }, *s = shrink;
  2049. gfp_t gfp = noreclaim;
  2050. do {
  2051. page = shmem_read_mapping_page_gfp(mapping, i, gfp);
  2052. if (likely(!IS_ERR(page)))
  2053. break;
  2054. if (!*s) {
  2055. ret = PTR_ERR(page);
  2056. goto err_sg;
  2057. }
  2058. i915_gem_shrink(dev_priv, 2 * page_count, NULL, *s++);
  2059. cond_resched();
  2060. /* We've tried hard to allocate the memory by reaping
  2061. * our own buffer, now let the real VM do its job and
  2062. * go down in flames if truly OOM.
  2063. *
  2064. * However, since graphics tend to be disposable,
  2065. * defer the oom here by reporting the ENOMEM back
  2066. * to userspace.
  2067. */
  2068. if (!*s) {
  2069. /* reclaim and warn, but no oom */
  2070. gfp = mapping_gfp_mask(mapping);
  2071. /* Our bo are always dirty and so we require
  2072. * kswapd to reclaim our pages (direct reclaim
  2073. * does not effectively begin pageout of our
  2074. * buffers on its own). However, direct reclaim
  2075. * only waits for kswapd when under allocation
  2076. * congestion. So as a result __GFP_RECLAIM is
  2077. * unreliable and fails to actually reclaim our
  2078. * dirty pages -- unless you try over and over
  2079. * again with !__GFP_NORETRY. However, we still
  2080. * want to fail this allocation rather than
  2081. * trigger the out-of-memory killer and for
  2082. * this we want __GFP_RETRY_MAYFAIL.
  2083. */
  2084. gfp |= __GFP_RETRY_MAYFAIL;
  2085. }
  2086. } while (1);
  2087. if (!i ||
  2088. sg->length >= max_segment ||
  2089. page_to_pfn(page) != last_pfn + 1) {
  2090. if (i) {
  2091. sg_page_sizes |= sg->length;
  2092. sg = sg_next(sg);
  2093. }
  2094. st->nents++;
  2095. sg_set_page(sg, page, PAGE_SIZE, 0);
  2096. } else {
  2097. sg->length += PAGE_SIZE;
  2098. }
  2099. last_pfn = page_to_pfn(page);
  2100. /* Check that the i965g/gm workaround works. */
  2101. WARN_ON((gfp & __GFP_DMA32) && (last_pfn >= 0x00100000UL));
  2102. }
  2103. if (sg) { /* loop terminated early; short sg table */
  2104. sg_page_sizes |= sg->length;
  2105. sg_mark_end(sg);
  2106. }
  2107. /* Trim unused sg entries to avoid wasting memory. */
  2108. i915_sg_trim(st);
  2109. ret = i915_gem_gtt_prepare_pages(obj, st);
  2110. if (ret) {
  2111. /* DMA remapping failed? One possible cause is that
  2112. * it could not reserve enough large entries, asking
  2113. * for PAGE_SIZE chunks instead may be helpful.
  2114. */
  2115. if (max_segment > PAGE_SIZE) {
  2116. for_each_sgt_page(page, sgt_iter, st)
  2117. put_page(page);
  2118. sg_free_table(st);
  2119. max_segment = PAGE_SIZE;
  2120. goto rebuild_st;
  2121. } else {
  2122. dev_warn(&dev_priv->drm.pdev->dev,
  2123. "Failed to DMA remap %lu pages\n",
  2124. page_count);
  2125. goto err_pages;
  2126. }
  2127. }
  2128. if (i915_gem_object_needs_bit17_swizzle(obj))
  2129. i915_gem_object_do_bit_17_swizzle(obj, st);
  2130. __i915_gem_object_set_pages(obj, st, sg_page_sizes);
  2131. return 0;
  2132. err_sg:
  2133. sg_mark_end(sg);
  2134. err_pages:
  2135. for_each_sgt_page(page, sgt_iter, st)
  2136. put_page(page);
  2137. sg_free_table(st);
  2138. kfree(st);
  2139. /* shmemfs first checks if there is enough memory to allocate the page
  2140. * and reports ENOSPC should there be insufficient, along with the usual
  2141. * ENOMEM for a genuine allocation failure.
  2142. *
  2143. * We use ENOSPC in our driver to mean that we have run out of aperture
  2144. * space and so want to translate the error from shmemfs back to our
  2145. * usual understanding of ENOMEM.
  2146. */
  2147. if (ret == -ENOSPC)
  2148. ret = -ENOMEM;
  2149. return ret;
  2150. }
  2151. void __i915_gem_object_set_pages(struct drm_i915_gem_object *obj,
  2152. struct sg_table *pages,
  2153. unsigned int sg_page_sizes)
  2154. {
  2155. struct drm_i915_private *i915 = to_i915(obj->base.dev);
  2156. unsigned long supported = INTEL_INFO(i915)->page_sizes;
  2157. int i;
  2158. lockdep_assert_held(&obj->mm.lock);
  2159. obj->mm.get_page.sg_pos = pages->sgl;
  2160. obj->mm.get_page.sg_idx = 0;
  2161. obj->mm.pages = pages;
  2162. if (i915_gem_object_is_tiled(obj) &&
  2163. i915->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
  2164. GEM_BUG_ON(obj->mm.quirked);
  2165. __i915_gem_object_pin_pages(obj);
  2166. obj->mm.quirked = true;
  2167. }
  2168. GEM_BUG_ON(!sg_page_sizes);
  2169. obj->mm.page_sizes.phys = sg_page_sizes;
  2170. /*
  2171. * Calculate the supported page-sizes which fit into the given
  2172. * sg_page_sizes. This will give us the page-sizes which we may be able
  2173. * to use opportunistically when later inserting into the GTT. For
  2174. * example if phys=2G, then in theory we should be able to use 1G, 2M,
  2175. * 64K or 4K pages, although in practice this will depend on a number of
  2176. * other factors.
  2177. */
  2178. obj->mm.page_sizes.sg = 0;
  2179. for_each_set_bit(i, &supported, ilog2(I915_GTT_MAX_PAGE_SIZE) + 1) {
  2180. if (obj->mm.page_sizes.phys & ~0u << i)
  2181. obj->mm.page_sizes.sg |= BIT(i);
  2182. }
  2183. GEM_BUG_ON(!HAS_PAGE_SIZES(i915, obj->mm.page_sizes.sg));
  2184. spin_lock(&i915->mm.obj_lock);
  2185. list_add(&obj->mm.link, &i915->mm.unbound_list);
  2186. spin_unlock(&i915->mm.obj_lock);
  2187. }
  2188. static int ____i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
  2189. {
  2190. int err;
  2191. if (unlikely(obj->mm.madv != I915_MADV_WILLNEED)) {
  2192. DRM_DEBUG("Attempting to obtain a purgeable object\n");
  2193. return -EFAULT;
  2194. }
  2195. err = obj->ops->get_pages(obj);
  2196. GEM_BUG_ON(!err && !i915_gem_object_has_pages(obj));
  2197. return err;
  2198. }
  2199. /* Ensure that the associated pages are gathered from the backing storage
  2200. * and pinned into our object. i915_gem_object_pin_pages() may be called
  2201. * multiple times before they are released by a single call to
  2202. * i915_gem_object_unpin_pages() - once the pages are no longer referenced
  2203. * either as a result of memory pressure (reaping pages under the shrinker)
  2204. * or as the object is itself released.
  2205. */
  2206. int __i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
  2207. {
  2208. int err;
  2209. err = mutex_lock_interruptible(&obj->mm.lock);
  2210. if (err)
  2211. return err;
  2212. if (unlikely(!i915_gem_object_has_pages(obj))) {
  2213. GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj));
  2214. err = ____i915_gem_object_get_pages(obj);
  2215. if (err)
  2216. goto unlock;
  2217. smp_mb__before_atomic();
  2218. }
  2219. atomic_inc(&obj->mm.pages_pin_count);
  2220. unlock:
  2221. mutex_unlock(&obj->mm.lock);
  2222. return err;
  2223. }
  2224. /* The 'mapping' part of i915_gem_object_pin_map() below */
  2225. static void *i915_gem_object_map(const struct drm_i915_gem_object *obj,
  2226. enum i915_map_type type)
  2227. {
  2228. unsigned long n_pages = obj->base.size >> PAGE_SHIFT;
  2229. struct sg_table *sgt = obj->mm.pages;
  2230. struct sgt_iter sgt_iter;
  2231. struct page *page;
  2232. struct page *stack_pages[32];
  2233. struct page **pages = stack_pages;
  2234. unsigned long i = 0;
  2235. pgprot_t pgprot;
  2236. void *addr;
  2237. /* A single page can always be kmapped */
  2238. if (n_pages == 1 && type == I915_MAP_WB)
  2239. return kmap(sg_page(sgt->sgl));
  2240. if (n_pages > ARRAY_SIZE(stack_pages)) {
  2241. /* Too big for stack -- allocate temporary array instead */
  2242. pages = kvmalloc_array(n_pages, sizeof(*pages), GFP_KERNEL);
  2243. if (!pages)
  2244. return NULL;
  2245. }
  2246. for_each_sgt_page(page, sgt_iter, sgt)
  2247. pages[i++] = page;
  2248. /* Check that we have the expected number of pages */
  2249. GEM_BUG_ON(i != n_pages);
  2250. switch (type) {
  2251. default:
  2252. MISSING_CASE(type);
  2253. /* fallthrough to use PAGE_KERNEL anyway */
  2254. case I915_MAP_WB:
  2255. pgprot = PAGE_KERNEL;
  2256. break;
  2257. case I915_MAP_WC:
  2258. pgprot = pgprot_writecombine(PAGE_KERNEL_IO);
  2259. break;
  2260. }
  2261. addr = vmap(pages, n_pages, 0, pgprot);
  2262. if (pages != stack_pages)
  2263. kvfree(pages);
  2264. return addr;
  2265. }
  2266. /* get, pin, and map the pages of the object into kernel space */
  2267. void *i915_gem_object_pin_map(struct drm_i915_gem_object *obj,
  2268. enum i915_map_type type)
  2269. {
  2270. enum i915_map_type has_type;
  2271. bool pinned;
  2272. void *ptr;
  2273. int ret;
  2274. if (unlikely(!i915_gem_object_has_struct_page(obj)))
  2275. return ERR_PTR(-ENXIO);
  2276. ret = mutex_lock_interruptible(&obj->mm.lock);
  2277. if (ret)
  2278. return ERR_PTR(ret);
  2279. pinned = !(type & I915_MAP_OVERRIDE);
  2280. type &= ~I915_MAP_OVERRIDE;
  2281. if (!atomic_inc_not_zero(&obj->mm.pages_pin_count)) {
  2282. if (unlikely(!i915_gem_object_has_pages(obj))) {
  2283. GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj));
  2284. ret = ____i915_gem_object_get_pages(obj);
  2285. if (ret)
  2286. goto err_unlock;
  2287. smp_mb__before_atomic();
  2288. }
  2289. atomic_inc(&obj->mm.pages_pin_count);
  2290. pinned = false;
  2291. }
  2292. GEM_BUG_ON(!i915_gem_object_has_pages(obj));
  2293. ptr = page_unpack_bits(obj->mm.mapping, &has_type);
  2294. if (ptr && has_type != type) {
  2295. if (pinned) {
  2296. ret = -EBUSY;
  2297. goto err_unpin;
  2298. }
  2299. if (is_vmalloc_addr(ptr))
  2300. vunmap(ptr);
  2301. else
  2302. kunmap(kmap_to_page(ptr));
  2303. ptr = obj->mm.mapping = NULL;
  2304. }
  2305. if (!ptr) {
  2306. ptr = i915_gem_object_map(obj, type);
  2307. if (!ptr) {
  2308. ret = -ENOMEM;
  2309. goto err_unpin;
  2310. }
  2311. obj->mm.mapping = page_pack_bits(ptr, type);
  2312. }
  2313. out_unlock:
  2314. mutex_unlock(&obj->mm.lock);
  2315. return ptr;
  2316. err_unpin:
  2317. atomic_dec(&obj->mm.pages_pin_count);
  2318. err_unlock:
  2319. ptr = ERR_PTR(ret);
  2320. goto out_unlock;
  2321. }
  2322. static int
  2323. i915_gem_object_pwrite_gtt(struct drm_i915_gem_object *obj,
  2324. const struct drm_i915_gem_pwrite *arg)
  2325. {
  2326. struct address_space *mapping = obj->base.filp->f_mapping;
  2327. char __user *user_data = u64_to_user_ptr(arg->data_ptr);
  2328. u64 remain, offset;
  2329. unsigned int pg;
  2330. /* Before we instantiate/pin the backing store for our use, we
  2331. * can prepopulate the shmemfs filp efficiently using a write into
  2332. * the pagecache. We avoid the penalty of instantiating all the
  2333. * pages, important if the user is just writing to a few and never
  2334. * uses the object on the GPU, and using a direct write into shmemfs
  2335. * allows it to avoid the cost of retrieving a page (either swapin
  2336. * or clearing-before-use) before it is overwritten.
  2337. */
  2338. if (i915_gem_object_has_pages(obj))
  2339. return -ENODEV;
  2340. if (obj->mm.madv != I915_MADV_WILLNEED)
  2341. return -EFAULT;
  2342. /* Before the pages are instantiated the object is treated as being
  2343. * in the CPU domain. The pages will be clflushed as required before
  2344. * use, and we can freely write into the pages directly. If userspace
  2345. * races pwrite with any other operation; corruption will ensue -
  2346. * that is userspace's prerogative!
  2347. */
  2348. remain = arg->size;
  2349. offset = arg->offset;
  2350. pg = offset_in_page(offset);
  2351. do {
  2352. unsigned int len, unwritten;
  2353. struct page *page;
  2354. void *data, *vaddr;
  2355. int err;
  2356. len = PAGE_SIZE - pg;
  2357. if (len > remain)
  2358. len = remain;
  2359. err = pagecache_write_begin(obj->base.filp, mapping,
  2360. offset, len, 0,
  2361. &page, &data);
  2362. if (err < 0)
  2363. return err;
  2364. vaddr = kmap(page);
  2365. unwritten = copy_from_user(vaddr + pg, user_data, len);
  2366. kunmap(page);
  2367. err = pagecache_write_end(obj->base.filp, mapping,
  2368. offset, len, len - unwritten,
  2369. page, data);
  2370. if (err < 0)
  2371. return err;
  2372. if (unwritten)
  2373. return -EFAULT;
  2374. remain -= len;
  2375. user_data += len;
  2376. offset += len;
  2377. pg = 0;
  2378. } while (remain);
  2379. return 0;
  2380. }
  2381. static bool ban_context(const struct i915_gem_context *ctx,
  2382. unsigned int score)
  2383. {
  2384. return (i915_gem_context_is_bannable(ctx) &&
  2385. score >= CONTEXT_SCORE_BAN_THRESHOLD);
  2386. }
  2387. static void i915_gem_context_mark_guilty(struct i915_gem_context *ctx)
  2388. {
  2389. unsigned int score;
  2390. bool banned;
  2391. atomic_inc(&ctx->guilty_count);
  2392. score = atomic_add_return(CONTEXT_SCORE_GUILTY, &ctx->ban_score);
  2393. banned = ban_context(ctx, score);
  2394. DRM_DEBUG_DRIVER("context %s marked guilty (score %d) banned? %s\n",
  2395. ctx->name, score, yesno(banned));
  2396. if (!banned)
  2397. return;
  2398. i915_gem_context_set_banned(ctx);
  2399. if (!IS_ERR_OR_NULL(ctx->file_priv)) {
  2400. atomic_inc(&ctx->file_priv->context_bans);
  2401. DRM_DEBUG_DRIVER("client %s has had %d context banned\n",
  2402. ctx->name, atomic_read(&ctx->file_priv->context_bans));
  2403. }
  2404. }
  2405. static void i915_gem_context_mark_innocent(struct i915_gem_context *ctx)
  2406. {
  2407. atomic_inc(&ctx->active_count);
  2408. }
  2409. struct drm_i915_gem_request *
  2410. i915_gem_find_active_request(struct intel_engine_cs *engine)
  2411. {
  2412. struct drm_i915_gem_request *request, *active = NULL;
  2413. unsigned long flags;
  2414. /* We are called by the error capture and reset at a random
  2415. * point in time. In particular, note that neither is crucially
  2416. * ordered with an interrupt. After a hang, the GPU is dead and we
  2417. * assume that no more writes can happen (we waited long enough for
  2418. * all writes that were in transaction to be flushed) - adding an
  2419. * extra delay for a recent interrupt is pointless. Hence, we do
  2420. * not need an engine->irq_seqno_barrier() before the seqno reads.
  2421. */
  2422. spin_lock_irqsave(&engine->timeline->lock, flags);
  2423. list_for_each_entry(request, &engine->timeline->requests, link) {
  2424. if (__i915_gem_request_completed(request,
  2425. request->global_seqno))
  2426. continue;
  2427. GEM_BUG_ON(request->engine != engine);
  2428. GEM_BUG_ON(test_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
  2429. &request->fence.flags));
  2430. active = request;
  2431. break;
  2432. }
  2433. spin_unlock_irqrestore(&engine->timeline->lock, flags);
  2434. return active;
  2435. }
  2436. static bool engine_stalled(struct intel_engine_cs *engine)
  2437. {
  2438. if (!engine->hangcheck.stalled)
  2439. return false;
  2440. /* Check for possible seqno movement after hang declaration */
  2441. if (engine->hangcheck.seqno != intel_engine_get_seqno(engine)) {
  2442. DRM_DEBUG_DRIVER("%s pardoned\n", engine->name);
  2443. return false;
  2444. }
  2445. return true;
  2446. }
  2447. /*
  2448. * Ensure irq handler finishes, and not run again.
  2449. * Also return the active request so that we only search for it once.
  2450. */
  2451. struct drm_i915_gem_request *
  2452. i915_gem_reset_prepare_engine(struct intel_engine_cs *engine)
  2453. {
  2454. struct drm_i915_gem_request *request = NULL;
  2455. /*
  2456. * During the reset sequence, we must prevent the engine from
  2457. * entering RC6. As the context state is undefined until we restart
  2458. * the engine, if it does enter RC6 during the reset, the state
  2459. * written to the powercontext is undefined and so we may lose
  2460. * GPU state upon resume, i.e. fail to restart after a reset.
  2461. */
  2462. intel_uncore_forcewake_get(engine->i915, FORCEWAKE_ALL);
  2463. /*
  2464. * Prevent the signaler thread from updating the request
  2465. * state (by calling dma_fence_signal) as we are processing
  2466. * the reset. The write from the GPU of the seqno is
  2467. * asynchronous and the signaler thread may see a different
  2468. * value to us and declare the request complete, even though
  2469. * the reset routine have picked that request as the active
  2470. * (incomplete) request. This conflict is not handled
  2471. * gracefully!
  2472. */
  2473. kthread_park(engine->breadcrumbs.signaler);
  2474. /*
  2475. * Prevent request submission to the hardware until we have
  2476. * completed the reset in i915_gem_reset_finish(). If a request
  2477. * is completed by one engine, it may then queue a request
  2478. * to a second via its execlists->tasklet *just* as we are
  2479. * calling engine->init_hw() and also writing the ELSP.
  2480. * Turning off the execlists->tasklet until the reset is over
  2481. * prevents the race.
  2482. */
  2483. tasklet_kill(&engine->execlists.tasklet);
  2484. tasklet_disable(&engine->execlists.tasklet);
  2485. /*
  2486. * We're using worker to queue preemption requests from the tasklet in
  2487. * GuC submission mode.
  2488. * Even though tasklet was disabled, we may still have a worker queued.
  2489. * Let's make sure that all workers scheduled before disabling the
  2490. * tasklet are completed before continuing with the reset.
  2491. */
  2492. if (engine->i915->guc.preempt_wq)
  2493. flush_workqueue(engine->i915->guc.preempt_wq);
  2494. if (engine->irq_seqno_barrier)
  2495. engine->irq_seqno_barrier(engine);
  2496. request = i915_gem_find_active_request(engine);
  2497. if (request && request->fence.error == -EIO)
  2498. request = ERR_PTR(-EIO); /* Previous reset failed! */
  2499. return request;
  2500. }
  2501. int i915_gem_reset_prepare(struct drm_i915_private *dev_priv)
  2502. {
  2503. struct intel_engine_cs *engine;
  2504. struct drm_i915_gem_request *request;
  2505. enum intel_engine_id id;
  2506. int err = 0;
  2507. for_each_engine(engine, dev_priv, id) {
  2508. request = i915_gem_reset_prepare_engine(engine);
  2509. if (IS_ERR(request)) {
  2510. err = PTR_ERR(request);
  2511. continue;
  2512. }
  2513. engine->hangcheck.active_request = request;
  2514. }
  2515. i915_gem_revoke_fences(dev_priv);
  2516. return err;
  2517. }
  2518. static void skip_request(struct drm_i915_gem_request *request)
  2519. {
  2520. void *vaddr = request->ring->vaddr;
  2521. u32 head;
  2522. /* As this request likely depends on state from the lost
  2523. * context, clear out all the user operations leaving the
  2524. * breadcrumb at the end (so we get the fence notifications).
  2525. */
  2526. head = request->head;
  2527. if (request->postfix < head) {
  2528. memset(vaddr + head, 0, request->ring->size - head);
  2529. head = 0;
  2530. }
  2531. memset(vaddr + head, 0, request->postfix - head);
  2532. dma_fence_set_error(&request->fence, -EIO);
  2533. }
  2534. static void engine_skip_context(struct drm_i915_gem_request *request)
  2535. {
  2536. struct intel_engine_cs *engine = request->engine;
  2537. struct i915_gem_context *hung_ctx = request->ctx;
  2538. struct intel_timeline *timeline;
  2539. unsigned long flags;
  2540. timeline = i915_gem_context_lookup_timeline(hung_ctx, engine);
  2541. spin_lock_irqsave(&engine->timeline->lock, flags);
  2542. spin_lock(&timeline->lock);
  2543. list_for_each_entry_continue(request, &engine->timeline->requests, link)
  2544. if (request->ctx == hung_ctx)
  2545. skip_request(request);
  2546. list_for_each_entry(request, &timeline->requests, link)
  2547. skip_request(request);
  2548. spin_unlock(&timeline->lock);
  2549. spin_unlock_irqrestore(&engine->timeline->lock, flags);
  2550. }
  2551. /* Returns the request if it was guilty of the hang */
  2552. static struct drm_i915_gem_request *
  2553. i915_gem_reset_request(struct intel_engine_cs *engine,
  2554. struct drm_i915_gem_request *request)
  2555. {
  2556. /* The guilty request will get skipped on a hung engine.
  2557. *
  2558. * Users of client default contexts do not rely on logical
  2559. * state preserved between batches so it is safe to execute
  2560. * queued requests following the hang. Non default contexts
  2561. * rely on preserved state, so skipping a batch loses the
  2562. * evolution of the state and it needs to be considered corrupted.
  2563. * Executing more queued batches on top of corrupted state is
  2564. * risky. But we take the risk by trying to advance through
  2565. * the queued requests in order to make the client behaviour
  2566. * more predictable around resets, by not throwing away random
  2567. * amount of batches it has prepared for execution. Sophisticated
  2568. * clients can use gem_reset_stats_ioctl and dma fence status
  2569. * (exported via sync_file info ioctl on explicit fences) to observe
  2570. * when it loses the context state and should rebuild accordingly.
  2571. *
  2572. * The context ban, and ultimately the client ban, mechanism are safety
  2573. * valves if client submission ends up resulting in nothing more than
  2574. * subsequent hangs.
  2575. */
  2576. if (engine_stalled(engine)) {
  2577. i915_gem_context_mark_guilty(request->ctx);
  2578. skip_request(request);
  2579. /* If this context is now banned, skip all pending requests. */
  2580. if (i915_gem_context_is_banned(request->ctx))
  2581. engine_skip_context(request);
  2582. } else {
  2583. /*
  2584. * Since this is not the hung engine, it may have advanced
  2585. * since the hang declaration. Double check by refinding
  2586. * the active request at the time of the reset.
  2587. */
  2588. request = i915_gem_find_active_request(engine);
  2589. if (request) {
  2590. i915_gem_context_mark_innocent(request->ctx);
  2591. dma_fence_set_error(&request->fence, -EAGAIN);
  2592. /* Rewind the engine to replay the incomplete rq */
  2593. spin_lock_irq(&engine->timeline->lock);
  2594. request = list_prev_entry(request, link);
  2595. if (&request->link == &engine->timeline->requests)
  2596. request = NULL;
  2597. spin_unlock_irq(&engine->timeline->lock);
  2598. }
  2599. }
  2600. return request;
  2601. }
  2602. void i915_gem_reset_engine(struct intel_engine_cs *engine,
  2603. struct drm_i915_gem_request *request)
  2604. {
  2605. /*
  2606. * Make sure this write is visible before we re-enable the interrupt
  2607. * handlers on another CPU, as tasklet_enable() resolves to just
  2608. * a compiler barrier which is insufficient for our purpose here.
  2609. */
  2610. smp_store_mb(engine->irq_posted, 0);
  2611. if (request)
  2612. request = i915_gem_reset_request(engine, request);
  2613. if (request) {
  2614. DRM_DEBUG_DRIVER("resetting %s to restart from tail of request 0x%x\n",
  2615. engine->name, request->global_seqno);
  2616. }
  2617. /* Setup the CS to resume from the breadcrumb of the hung request */
  2618. engine->reset_hw(engine, request);
  2619. }
  2620. void i915_gem_reset(struct drm_i915_private *dev_priv)
  2621. {
  2622. struct intel_engine_cs *engine;
  2623. enum intel_engine_id id;
  2624. lockdep_assert_held(&dev_priv->drm.struct_mutex);
  2625. i915_gem_retire_requests(dev_priv);
  2626. for_each_engine(engine, dev_priv, id) {
  2627. struct i915_gem_context *ctx;
  2628. i915_gem_reset_engine(engine, engine->hangcheck.active_request);
  2629. ctx = fetch_and_zero(&engine->last_retired_context);
  2630. if (ctx)
  2631. engine->context_unpin(engine, ctx);
  2632. /*
  2633. * Ostensibily, we always want a context loaded for powersaving,
  2634. * so if the engine is idle after the reset, send a request
  2635. * to load our scratch kernel_context.
  2636. *
  2637. * More mysteriously, if we leave the engine idle after a reset,
  2638. * the next userspace batch may hang, with what appears to be
  2639. * an incoherent read by the CS (presumably stale TLB). An
  2640. * empty request appears sufficient to paper over the glitch.
  2641. */
  2642. if (list_empty(&engine->timeline->requests)) {
  2643. struct drm_i915_gem_request *rq;
  2644. rq = i915_gem_request_alloc(engine,
  2645. dev_priv->kernel_context);
  2646. if (!IS_ERR(rq))
  2647. __i915_add_request(rq, false);
  2648. }
  2649. }
  2650. i915_gem_restore_fences(dev_priv);
  2651. if (dev_priv->gt.awake) {
  2652. intel_sanitize_gt_powersave(dev_priv);
  2653. intel_enable_gt_powersave(dev_priv);
  2654. if (INTEL_GEN(dev_priv) >= 6)
  2655. gen6_rps_busy(dev_priv);
  2656. }
  2657. }
  2658. void i915_gem_reset_finish_engine(struct intel_engine_cs *engine)
  2659. {
  2660. tasklet_enable(&engine->execlists.tasklet);
  2661. kthread_unpark(engine->breadcrumbs.signaler);
  2662. intel_uncore_forcewake_put(engine->i915, FORCEWAKE_ALL);
  2663. }
  2664. void i915_gem_reset_finish(struct drm_i915_private *dev_priv)
  2665. {
  2666. struct intel_engine_cs *engine;
  2667. enum intel_engine_id id;
  2668. lockdep_assert_held(&dev_priv->drm.struct_mutex);
  2669. for_each_engine(engine, dev_priv, id) {
  2670. engine->hangcheck.active_request = NULL;
  2671. i915_gem_reset_finish_engine(engine);
  2672. }
  2673. }
  2674. static void nop_submit_request(struct drm_i915_gem_request *request)
  2675. {
  2676. dma_fence_set_error(&request->fence, -EIO);
  2677. i915_gem_request_submit(request);
  2678. }
  2679. static void nop_complete_submit_request(struct drm_i915_gem_request *request)
  2680. {
  2681. unsigned long flags;
  2682. dma_fence_set_error(&request->fence, -EIO);
  2683. spin_lock_irqsave(&request->engine->timeline->lock, flags);
  2684. __i915_gem_request_submit(request);
  2685. intel_engine_init_global_seqno(request->engine, request->global_seqno);
  2686. spin_unlock_irqrestore(&request->engine->timeline->lock, flags);
  2687. }
  2688. void i915_gem_set_wedged(struct drm_i915_private *i915)
  2689. {
  2690. struct intel_engine_cs *engine;
  2691. enum intel_engine_id id;
  2692. /*
  2693. * First, stop submission to hw, but do not yet complete requests by
  2694. * rolling the global seqno forward (since this would complete requests
  2695. * for which we haven't set the fence error to EIO yet).
  2696. */
  2697. for_each_engine(engine, i915, id)
  2698. engine->submit_request = nop_submit_request;
  2699. /*
  2700. * Make sure no one is running the old callback before we proceed with
  2701. * cancelling requests and resetting the completion tracking. Otherwise
  2702. * we might submit a request to the hardware which never completes.
  2703. */
  2704. synchronize_rcu();
  2705. for_each_engine(engine, i915, id) {
  2706. /* Mark all executing requests as skipped */
  2707. engine->cancel_requests(engine);
  2708. /*
  2709. * Only once we've force-cancelled all in-flight requests can we
  2710. * start to complete all requests.
  2711. */
  2712. engine->submit_request = nop_complete_submit_request;
  2713. }
  2714. /*
  2715. * Make sure no request can slip through without getting completed by
  2716. * either this call here to intel_engine_init_global_seqno, or the one
  2717. * in nop_complete_submit_request.
  2718. */
  2719. synchronize_rcu();
  2720. for_each_engine(engine, i915, id) {
  2721. unsigned long flags;
  2722. /* Mark all pending requests as complete so that any concurrent
  2723. * (lockless) lookup doesn't try and wait upon the request as we
  2724. * reset it.
  2725. */
  2726. spin_lock_irqsave(&engine->timeline->lock, flags);
  2727. intel_engine_init_global_seqno(engine,
  2728. intel_engine_last_submit(engine));
  2729. spin_unlock_irqrestore(&engine->timeline->lock, flags);
  2730. }
  2731. set_bit(I915_WEDGED, &i915->gpu_error.flags);
  2732. wake_up_all(&i915->gpu_error.reset_queue);
  2733. }
  2734. bool i915_gem_unset_wedged(struct drm_i915_private *i915)
  2735. {
  2736. struct i915_gem_timeline *tl;
  2737. int i;
  2738. lockdep_assert_held(&i915->drm.struct_mutex);
  2739. if (!test_bit(I915_WEDGED, &i915->gpu_error.flags))
  2740. return true;
  2741. /* Before unwedging, make sure that all pending operations
  2742. * are flushed and errored out - we may have requests waiting upon
  2743. * third party fences. We marked all inflight requests as EIO, and
  2744. * every execbuf since returned EIO, for consistency we want all
  2745. * the currently pending requests to also be marked as EIO, which
  2746. * is done inside our nop_submit_request - and so we must wait.
  2747. *
  2748. * No more can be submitted until we reset the wedged bit.
  2749. */
  2750. list_for_each_entry(tl, &i915->gt.timelines, link) {
  2751. for (i = 0; i < ARRAY_SIZE(tl->engine); i++) {
  2752. struct drm_i915_gem_request *rq;
  2753. rq = i915_gem_active_peek(&tl->engine[i].last_request,
  2754. &i915->drm.struct_mutex);
  2755. if (!rq)
  2756. continue;
  2757. /* We can't use our normal waiter as we want to
  2758. * avoid recursively trying to handle the current
  2759. * reset. The basic dma_fence_default_wait() installs
  2760. * a callback for dma_fence_signal(), which is
  2761. * triggered by our nop handler (indirectly, the
  2762. * callback enables the signaler thread which is
  2763. * woken by the nop_submit_request() advancing the seqno
  2764. * and when the seqno passes the fence, the signaler
  2765. * then signals the fence waking us up).
  2766. */
  2767. if (dma_fence_default_wait(&rq->fence, true,
  2768. MAX_SCHEDULE_TIMEOUT) < 0)
  2769. return false;
  2770. }
  2771. }
  2772. /* Undo nop_submit_request. We prevent all new i915 requests from
  2773. * being queued (by disallowing execbuf whilst wedged) so having
  2774. * waited for all active requests above, we know the system is idle
  2775. * and do not have to worry about a thread being inside
  2776. * engine->submit_request() as we swap over. So unlike installing
  2777. * the nop_submit_request on reset, we can do this from normal
  2778. * context and do not require stop_machine().
  2779. */
  2780. intel_engines_reset_default_submission(i915);
  2781. i915_gem_contexts_lost(i915);
  2782. smp_mb__before_atomic(); /* complete takeover before enabling execbuf */
  2783. clear_bit(I915_WEDGED, &i915->gpu_error.flags);
  2784. return true;
  2785. }
  2786. static void
  2787. i915_gem_retire_work_handler(struct work_struct *work)
  2788. {
  2789. struct drm_i915_private *dev_priv =
  2790. container_of(work, typeof(*dev_priv), gt.retire_work.work);
  2791. struct drm_device *dev = &dev_priv->drm;
  2792. /* Come back later if the device is busy... */
  2793. if (mutex_trylock(&dev->struct_mutex)) {
  2794. i915_gem_retire_requests(dev_priv);
  2795. mutex_unlock(&dev->struct_mutex);
  2796. }
  2797. /*
  2798. * Keep the retire handler running until we are finally idle.
  2799. * We do not need to do this test under locking as in the worst-case
  2800. * we queue the retire worker once too often.
  2801. */
  2802. if (READ_ONCE(dev_priv->gt.awake))
  2803. queue_delayed_work(dev_priv->wq,
  2804. &dev_priv->gt.retire_work,
  2805. round_jiffies_up_relative(HZ));
  2806. }
  2807. static inline bool
  2808. new_requests_since_last_retire(const struct drm_i915_private *i915)
  2809. {
  2810. return (READ_ONCE(i915->gt.active_requests) ||
  2811. work_pending(&i915->gt.idle_work.work));
  2812. }
  2813. static void
  2814. i915_gem_idle_work_handler(struct work_struct *work)
  2815. {
  2816. struct drm_i915_private *dev_priv =
  2817. container_of(work, typeof(*dev_priv), gt.idle_work.work);
  2818. bool rearm_hangcheck;
  2819. ktime_t end;
  2820. if (!READ_ONCE(dev_priv->gt.awake))
  2821. return;
  2822. /*
  2823. * Wait for last execlists context complete, but bail out in case a
  2824. * new request is submitted.
  2825. */
  2826. end = ktime_add_ms(ktime_get(), I915_IDLE_ENGINES_TIMEOUT);
  2827. do {
  2828. if (new_requests_since_last_retire(dev_priv))
  2829. return;
  2830. if (intel_engines_are_idle(dev_priv))
  2831. break;
  2832. usleep_range(100, 500);
  2833. } while (ktime_before(ktime_get(), end));
  2834. rearm_hangcheck =
  2835. cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
  2836. if (!mutex_trylock(&dev_priv->drm.struct_mutex)) {
  2837. /* Currently busy, come back later */
  2838. mod_delayed_work(dev_priv->wq,
  2839. &dev_priv->gt.idle_work,
  2840. msecs_to_jiffies(50));
  2841. goto out_rearm;
  2842. }
  2843. /*
  2844. * New request retired after this work handler started, extend active
  2845. * period until next instance of the work.
  2846. */
  2847. if (new_requests_since_last_retire(dev_priv))
  2848. goto out_unlock;
  2849. /*
  2850. * Be paranoid and flush a concurrent interrupt to make sure
  2851. * we don't reactivate any irq tasklets after parking.
  2852. *
  2853. * FIXME: Note that even though we have waited for execlists to be idle,
  2854. * there may still be an in-flight interrupt even though the CSB
  2855. * is now empty. synchronize_irq() makes sure that a residual interrupt
  2856. * is completed before we continue, but it doesn't prevent the HW from
  2857. * raising a spurious interrupt later. To complete the shield we should
  2858. * coordinate disabling the CS irq with flushing the interrupts.
  2859. */
  2860. synchronize_irq(dev_priv->drm.irq);
  2861. intel_engines_park(dev_priv);
  2862. i915_gem_timelines_park(dev_priv);
  2863. i915_pmu_gt_parked(dev_priv);
  2864. GEM_BUG_ON(!dev_priv->gt.awake);
  2865. dev_priv->gt.awake = false;
  2866. rearm_hangcheck = false;
  2867. if (INTEL_GEN(dev_priv) >= 6)
  2868. gen6_rps_idle(dev_priv);
  2869. intel_display_power_put(dev_priv, POWER_DOMAIN_GT_IRQ);
  2870. intel_runtime_pm_put(dev_priv);
  2871. out_unlock:
  2872. mutex_unlock(&dev_priv->drm.struct_mutex);
  2873. out_rearm:
  2874. if (rearm_hangcheck) {
  2875. GEM_BUG_ON(!dev_priv->gt.awake);
  2876. i915_queue_hangcheck(dev_priv);
  2877. }
  2878. }
  2879. void i915_gem_close_object(struct drm_gem_object *gem, struct drm_file *file)
  2880. {
  2881. struct drm_i915_private *i915 = to_i915(gem->dev);
  2882. struct drm_i915_gem_object *obj = to_intel_bo(gem);
  2883. struct drm_i915_file_private *fpriv = file->driver_priv;
  2884. struct i915_lut_handle *lut, *ln;
  2885. mutex_lock(&i915->drm.struct_mutex);
  2886. list_for_each_entry_safe(lut, ln, &obj->lut_list, obj_link) {
  2887. struct i915_gem_context *ctx = lut->ctx;
  2888. struct i915_vma *vma;
  2889. GEM_BUG_ON(ctx->file_priv == ERR_PTR(-EBADF));
  2890. if (ctx->file_priv != fpriv)
  2891. continue;
  2892. vma = radix_tree_delete(&ctx->handles_vma, lut->handle);
  2893. GEM_BUG_ON(vma->obj != obj);
  2894. /* We allow the process to have multiple handles to the same
  2895. * vma, in the same fd namespace, by virtue of flink/open.
  2896. */
  2897. GEM_BUG_ON(!vma->open_count);
  2898. if (!--vma->open_count && !i915_vma_is_ggtt(vma))
  2899. i915_vma_close(vma);
  2900. list_del(&lut->obj_link);
  2901. list_del(&lut->ctx_link);
  2902. kmem_cache_free(i915->luts, lut);
  2903. __i915_gem_object_release_unless_active(obj);
  2904. }
  2905. mutex_unlock(&i915->drm.struct_mutex);
  2906. }
  2907. static unsigned long to_wait_timeout(s64 timeout_ns)
  2908. {
  2909. if (timeout_ns < 0)
  2910. return MAX_SCHEDULE_TIMEOUT;
  2911. if (timeout_ns == 0)
  2912. return 0;
  2913. return nsecs_to_jiffies_timeout(timeout_ns);
  2914. }
  2915. /**
  2916. * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT
  2917. * @dev: drm device pointer
  2918. * @data: ioctl data blob
  2919. * @file: drm file pointer
  2920. *
  2921. * Returns 0 if successful, else an error is returned with the remaining time in
  2922. * the timeout parameter.
  2923. * -ETIME: object is still busy after timeout
  2924. * -ERESTARTSYS: signal interrupted the wait
  2925. * -ENONENT: object doesn't exist
  2926. * Also possible, but rare:
  2927. * -EAGAIN: incomplete, restart syscall
  2928. * -ENOMEM: damn
  2929. * -ENODEV: Internal IRQ fail
  2930. * -E?: The add request failed
  2931. *
  2932. * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any
  2933. * non-zero timeout parameter the wait ioctl will wait for the given number of
  2934. * nanoseconds on an object becoming unbusy. Since the wait itself does so
  2935. * without holding struct_mutex the object may become re-busied before this
  2936. * function completes. A similar but shorter * race condition exists in the busy
  2937. * ioctl
  2938. */
  2939. int
  2940. i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
  2941. {
  2942. struct drm_i915_gem_wait *args = data;
  2943. struct drm_i915_gem_object *obj;
  2944. ktime_t start;
  2945. long ret;
  2946. if (args->flags != 0)
  2947. return -EINVAL;
  2948. obj = i915_gem_object_lookup(file, args->bo_handle);
  2949. if (!obj)
  2950. return -ENOENT;
  2951. start = ktime_get();
  2952. ret = i915_gem_object_wait(obj,
  2953. I915_WAIT_INTERRUPTIBLE | I915_WAIT_ALL,
  2954. to_wait_timeout(args->timeout_ns),
  2955. to_rps_client(file));
  2956. if (args->timeout_ns > 0) {
  2957. args->timeout_ns -= ktime_to_ns(ktime_sub(ktime_get(), start));
  2958. if (args->timeout_ns < 0)
  2959. args->timeout_ns = 0;
  2960. /*
  2961. * Apparently ktime isn't accurate enough and occasionally has a
  2962. * bit of mismatch in the jiffies<->nsecs<->ktime loop. So patch
  2963. * things up to make the test happy. We allow up to 1 jiffy.
  2964. *
  2965. * This is a regression from the timespec->ktime conversion.
  2966. */
  2967. if (ret == -ETIME && !nsecs_to_jiffies(args->timeout_ns))
  2968. args->timeout_ns = 0;
  2969. /* Asked to wait beyond the jiffie/scheduler precision? */
  2970. if (ret == -ETIME && args->timeout_ns)
  2971. ret = -EAGAIN;
  2972. }
  2973. i915_gem_object_put(obj);
  2974. return ret;
  2975. }
  2976. static int wait_for_timeline(struct i915_gem_timeline *tl, unsigned int flags)
  2977. {
  2978. int ret, i;
  2979. for (i = 0; i < ARRAY_SIZE(tl->engine); i++) {
  2980. ret = i915_gem_active_wait(&tl->engine[i].last_request, flags);
  2981. if (ret)
  2982. return ret;
  2983. }
  2984. return 0;
  2985. }
  2986. static int wait_for_engines(struct drm_i915_private *i915)
  2987. {
  2988. if (wait_for(intel_engines_are_idle(i915), I915_IDLE_ENGINES_TIMEOUT)) {
  2989. dev_err(i915->drm.dev,
  2990. "Failed to idle engines, declaring wedged!\n");
  2991. if (drm_debug & DRM_UT_DRIVER) {
  2992. struct drm_printer p = drm_debug_printer(__func__);
  2993. struct intel_engine_cs *engine;
  2994. enum intel_engine_id id;
  2995. for_each_engine(engine, i915, id)
  2996. intel_engine_dump(engine, &p,
  2997. "%s", engine->name);
  2998. }
  2999. i915_gem_set_wedged(i915);
  3000. return -EIO;
  3001. }
  3002. return 0;
  3003. }
  3004. int i915_gem_wait_for_idle(struct drm_i915_private *i915, unsigned int flags)
  3005. {
  3006. int ret;
  3007. /* If the device is asleep, we have no requests outstanding */
  3008. if (!READ_ONCE(i915->gt.awake))
  3009. return 0;
  3010. if (flags & I915_WAIT_LOCKED) {
  3011. struct i915_gem_timeline *tl;
  3012. lockdep_assert_held(&i915->drm.struct_mutex);
  3013. list_for_each_entry(tl, &i915->gt.timelines, link) {
  3014. ret = wait_for_timeline(tl, flags);
  3015. if (ret)
  3016. return ret;
  3017. }
  3018. i915_gem_retire_requests(i915);
  3019. ret = wait_for_engines(i915);
  3020. } else {
  3021. ret = wait_for_timeline(&i915->gt.global_timeline, flags);
  3022. }
  3023. return ret;
  3024. }
  3025. static void __i915_gem_object_flush_for_display(struct drm_i915_gem_object *obj)
  3026. {
  3027. /*
  3028. * We manually flush the CPU domain so that we can override and
  3029. * force the flush for the display, and perform it asyncrhonously.
  3030. */
  3031. flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
  3032. if (obj->cache_dirty)
  3033. i915_gem_clflush_object(obj, I915_CLFLUSH_FORCE);
  3034. obj->base.write_domain = 0;
  3035. }
  3036. void i915_gem_object_flush_if_display(struct drm_i915_gem_object *obj)
  3037. {
  3038. if (!READ_ONCE(obj->pin_global))
  3039. return;
  3040. mutex_lock(&obj->base.dev->struct_mutex);
  3041. __i915_gem_object_flush_for_display(obj);
  3042. mutex_unlock(&obj->base.dev->struct_mutex);
  3043. }
  3044. /**
  3045. * Moves a single object to the WC read, and possibly write domain.
  3046. * @obj: object to act on
  3047. * @write: ask for write access or read only
  3048. *
  3049. * This function returns when the move is complete, including waiting on
  3050. * flushes to occur.
  3051. */
  3052. int
  3053. i915_gem_object_set_to_wc_domain(struct drm_i915_gem_object *obj, bool write)
  3054. {
  3055. int ret;
  3056. lockdep_assert_held(&obj->base.dev->struct_mutex);
  3057. ret = i915_gem_object_wait(obj,
  3058. I915_WAIT_INTERRUPTIBLE |
  3059. I915_WAIT_LOCKED |
  3060. (write ? I915_WAIT_ALL : 0),
  3061. MAX_SCHEDULE_TIMEOUT,
  3062. NULL);
  3063. if (ret)
  3064. return ret;
  3065. if (obj->base.write_domain == I915_GEM_DOMAIN_WC)
  3066. return 0;
  3067. /* Flush and acquire obj->pages so that we are coherent through
  3068. * direct access in memory with previous cached writes through
  3069. * shmemfs and that our cache domain tracking remains valid.
  3070. * For example, if the obj->filp was moved to swap without us
  3071. * being notified and releasing the pages, we would mistakenly
  3072. * continue to assume that the obj remained out of the CPU cached
  3073. * domain.
  3074. */
  3075. ret = i915_gem_object_pin_pages(obj);
  3076. if (ret)
  3077. return ret;
  3078. flush_write_domain(obj, ~I915_GEM_DOMAIN_WC);
  3079. /* Serialise direct access to this object with the barriers for
  3080. * coherent writes from the GPU, by effectively invalidating the
  3081. * WC domain upon first access.
  3082. */
  3083. if ((obj->base.read_domains & I915_GEM_DOMAIN_WC) == 0)
  3084. mb();
  3085. /* It should now be out of any other write domains, and we can update
  3086. * the domain values for our changes.
  3087. */
  3088. GEM_BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_WC) != 0);
  3089. obj->base.read_domains |= I915_GEM_DOMAIN_WC;
  3090. if (write) {
  3091. obj->base.read_domains = I915_GEM_DOMAIN_WC;
  3092. obj->base.write_domain = I915_GEM_DOMAIN_WC;
  3093. obj->mm.dirty = true;
  3094. }
  3095. i915_gem_object_unpin_pages(obj);
  3096. return 0;
  3097. }
  3098. /**
  3099. * Moves a single object to the GTT read, and possibly write domain.
  3100. * @obj: object to act on
  3101. * @write: ask for write access or read only
  3102. *
  3103. * This function returns when the move is complete, including waiting on
  3104. * flushes to occur.
  3105. */
  3106. int
  3107. i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
  3108. {
  3109. int ret;
  3110. lockdep_assert_held(&obj->base.dev->struct_mutex);
  3111. ret = i915_gem_object_wait(obj,
  3112. I915_WAIT_INTERRUPTIBLE |
  3113. I915_WAIT_LOCKED |
  3114. (write ? I915_WAIT_ALL : 0),
  3115. MAX_SCHEDULE_TIMEOUT,
  3116. NULL);
  3117. if (ret)
  3118. return ret;
  3119. if (obj->base.write_domain == I915_GEM_DOMAIN_GTT)
  3120. return 0;
  3121. /* Flush and acquire obj->pages so that we are coherent through
  3122. * direct access in memory with previous cached writes through
  3123. * shmemfs and that our cache domain tracking remains valid.
  3124. * For example, if the obj->filp was moved to swap without us
  3125. * being notified and releasing the pages, we would mistakenly
  3126. * continue to assume that the obj remained out of the CPU cached
  3127. * domain.
  3128. */
  3129. ret = i915_gem_object_pin_pages(obj);
  3130. if (ret)
  3131. return ret;
  3132. flush_write_domain(obj, ~I915_GEM_DOMAIN_GTT);
  3133. /* Serialise direct access to this object with the barriers for
  3134. * coherent writes from the GPU, by effectively invalidating the
  3135. * GTT domain upon first access.
  3136. */
  3137. if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
  3138. mb();
  3139. /* It should now be out of any other write domains, and we can update
  3140. * the domain values for our changes.
  3141. */
  3142. GEM_BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
  3143. obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
  3144. if (write) {
  3145. obj->base.read_domains = I915_GEM_DOMAIN_GTT;
  3146. obj->base.write_domain = I915_GEM_DOMAIN_GTT;
  3147. obj->mm.dirty = true;
  3148. }
  3149. i915_gem_object_unpin_pages(obj);
  3150. return 0;
  3151. }
  3152. /**
  3153. * Changes the cache-level of an object across all VMA.
  3154. * @obj: object to act on
  3155. * @cache_level: new cache level to set for the object
  3156. *
  3157. * After this function returns, the object will be in the new cache-level
  3158. * across all GTT and the contents of the backing storage will be coherent,
  3159. * with respect to the new cache-level. In order to keep the backing storage
  3160. * coherent for all users, we only allow a single cache level to be set
  3161. * globally on the object and prevent it from being changed whilst the
  3162. * hardware is reading from the object. That is if the object is currently
  3163. * on the scanout it will be set to uncached (or equivalent display
  3164. * cache coherency) and all non-MOCS GPU access will also be uncached so
  3165. * that all direct access to the scanout remains coherent.
  3166. */
  3167. int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
  3168. enum i915_cache_level cache_level)
  3169. {
  3170. struct i915_vma *vma;
  3171. int ret;
  3172. lockdep_assert_held(&obj->base.dev->struct_mutex);
  3173. if (obj->cache_level == cache_level)
  3174. return 0;
  3175. /* Inspect the list of currently bound VMA and unbind any that would
  3176. * be invalid given the new cache-level. This is principally to
  3177. * catch the issue of the CS prefetch crossing page boundaries and
  3178. * reading an invalid PTE on older architectures.
  3179. */
  3180. restart:
  3181. list_for_each_entry(vma, &obj->vma_list, obj_link) {
  3182. if (!drm_mm_node_allocated(&vma->node))
  3183. continue;
  3184. if (i915_vma_is_pinned(vma)) {
  3185. DRM_DEBUG("can not change the cache level of pinned objects\n");
  3186. return -EBUSY;
  3187. }
  3188. if (!i915_vma_is_closed(vma) &&
  3189. i915_gem_valid_gtt_space(vma, cache_level))
  3190. continue;
  3191. ret = i915_vma_unbind(vma);
  3192. if (ret)
  3193. return ret;
  3194. /* As unbinding may affect other elements in the
  3195. * obj->vma_list (due to side-effects from retiring
  3196. * an active vma), play safe and restart the iterator.
  3197. */
  3198. goto restart;
  3199. }
  3200. /* We can reuse the existing drm_mm nodes but need to change the
  3201. * cache-level on the PTE. We could simply unbind them all and
  3202. * rebind with the correct cache-level on next use. However since
  3203. * we already have a valid slot, dma mapping, pages etc, we may as
  3204. * rewrite the PTE in the belief that doing so tramples upon less
  3205. * state and so involves less work.
  3206. */
  3207. if (obj->bind_count) {
  3208. /* Before we change the PTE, the GPU must not be accessing it.
  3209. * If we wait upon the object, we know that all the bound
  3210. * VMA are no longer active.
  3211. */
  3212. ret = i915_gem_object_wait(obj,
  3213. I915_WAIT_INTERRUPTIBLE |
  3214. I915_WAIT_LOCKED |
  3215. I915_WAIT_ALL,
  3216. MAX_SCHEDULE_TIMEOUT,
  3217. NULL);
  3218. if (ret)
  3219. return ret;
  3220. if (!HAS_LLC(to_i915(obj->base.dev)) &&
  3221. cache_level != I915_CACHE_NONE) {
  3222. /* Access to snoopable pages through the GTT is
  3223. * incoherent and on some machines causes a hard
  3224. * lockup. Relinquish the CPU mmaping to force
  3225. * userspace to refault in the pages and we can
  3226. * then double check if the GTT mapping is still
  3227. * valid for that pointer access.
  3228. */
  3229. i915_gem_release_mmap(obj);
  3230. /* As we no longer need a fence for GTT access,
  3231. * we can relinquish it now (and so prevent having
  3232. * to steal a fence from someone else on the next
  3233. * fence request). Note GPU activity would have
  3234. * dropped the fence as all snoopable access is
  3235. * supposed to be linear.
  3236. */
  3237. for_each_ggtt_vma(vma, obj) {
  3238. ret = i915_vma_put_fence(vma);
  3239. if (ret)
  3240. return ret;
  3241. }
  3242. } else {
  3243. /* We either have incoherent backing store and
  3244. * so no GTT access or the architecture is fully
  3245. * coherent. In such cases, existing GTT mmaps
  3246. * ignore the cache bit in the PTE and we can
  3247. * rewrite it without confusing the GPU or having
  3248. * to force userspace to fault back in its mmaps.
  3249. */
  3250. }
  3251. list_for_each_entry(vma, &obj->vma_list, obj_link) {
  3252. if (!drm_mm_node_allocated(&vma->node))
  3253. continue;
  3254. ret = i915_vma_bind(vma, cache_level, PIN_UPDATE);
  3255. if (ret)
  3256. return ret;
  3257. }
  3258. }
  3259. list_for_each_entry(vma, &obj->vma_list, obj_link)
  3260. vma->node.color = cache_level;
  3261. i915_gem_object_set_cache_coherency(obj, cache_level);
  3262. obj->cache_dirty = true; /* Always invalidate stale cachelines */
  3263. return 0;
  3264. }
  3265. int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data,
  3266. struct drm_file *file)
  3267. {
  3268. struct drm_i915_gem_caching *args = data;
  3269. struct drm_i915_gem_object *obj;
  3270. int err = 0;
  3271. rcu_read_lock();
  3272. obj = i915_gem_object_lookup_rcu(file, args->handle);
  3273. if (!obj) {
  3274. err = -ENOENT;
  3275. goto out;
  3276. }
  3277. switch (obj->cache_level) {
  3278. case I915_CACHE_LLC:
  3279. case I915_CACHE_L3_LLC:
  3280. args->caching = I915_CACHING_CACHED;
  3281. break;
  3282. case I915_CACHE_WT:
  3283. args->caching = I915_CACHING_DISPLAY;
  3284. break;
  3285. default:
  3286. args->caching = I915_CACHING_NONE;
  3287. break;
  3288. }
  3289. out:
  3290. rcu_read_unlock();
  3291. return err;
  3292. }
  3293. int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data,
  3294. struct drm_file *file)
  3295. {
  3296. struct drm_i915_private *i915 = to_i915(dev);
  3297. struct drm_i915_gem_caching *args = data;
  3298. struct drm_i915_gem_object *obj;
  3299. enum i915_cache_level level;
  3300. int ret = 0;
  3301. switch (args->caching) {
  3302. case I915_CACHING_NONE:
  3303. level = I915_CACHE_NONE;
  3304. break;
  3305. case I915_CACHING_CACHED:
  3306. /*
  3307. * Due to a HW issue on BXT A stepping, GPU stores via a
  3308. * snooped mapping may leave stale data in a corresponding CPU
  3309. * cacheline, whereas normally such cachelines would get
  3310. * invalidated.
  3311. */
  3312. if (!HAS_LLC(i915) && !HAS_SNOOP(i915))
  3313. return -ENODEV;
  3314. level = I915_CACHE_LLC;
  3315. break;
  3316. case I915_CACHING_DISPLAY:
  3317. level = HAS_WT(i915) ? I915_CACHE_WT : I915_CACHE_NONE;
  3318. break;
  3319. default:
  3320. return -EINVAL;
  3321. }
  3322. obj = i915_gem_object_lookup(file, args->handle);
  3323. if (!obj)
  3324. return -ENOENT;
  3325. /*
  3326. * The caching mode of proxy object is handled by its generator, and
  3327. * not allowed to be changed by userspace.
  3328. */
  3329. if (i915_gem_object_is_proxy(obj)) {
  3330. ret = -ENXIO;
  3331. goto out;
  3332. }
  3333. if (obj->cache_level == level)
  3334. goto out;
  3335. ret = i915_gem_object_wait(obj,
  3336. I915_WAIT_INTERRUPTIBLE,
  3337. MAX_SCHEDULE_TIMEOUT,
  3338. to_rps_client(file));
  3339. if (ret)
  3340. goto out;
  3341. ret = i915_mutex_lock_interruptible(dev);
  3342. if (ret)
  3343. goto out;
  3344. ret = i915_gem_object_set_cache_level(obj, level);
  3345. mutex_unlock(&dev->struct_mutex);
  3346. out:
  3347. i915_gem_object_put(obj);
  3348. return ret;
  3349. }
  3350. /*
  3351. * Prepare buffer for display plane (scanout, cursors, etc).
  3352. * Can be called from an uninterruptible phase (modesetting) and allows
  3353. * any flushes to be pipelined (for pageflips).
  3354. */
  3355. struct i915_vma *
  3356. i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
  3357. u32 alignment,
  3358. const struct i915_ggtt_view *view)
  3359. {
  3360. struct i915_vma *vma;
  3361. int ret;
  3362. lockdep_assert_held(&obj->base.dev->struct_mutex);
  3363. /* Mark the global pin early so that we account for the
  3364. * display coherency whilst setting up the cache domains.
  3365. */
  3366. obj->pin_global++;
  3367. /* The display engine is not coherent with the LLC cache on gen6. As
  3368. * a result, we make sure that the pinning that is about to occur is
  3369. * done with uncached PTEs. This is lowest common denominator for all
  3370. * chipsets.
  3371. *
  3372. * However for gen6+, we could do better by using the GFDT bit instead
  3373. * of uncaching, which would allow us to flush all the LLC-cached data
  3374. * with that bit in the PTE to main memory with just one PIPE_CONTROL.
  3375. */
  3376. ret = i915_gem_object_set_cache_level(obj,
  3377. HAS_WT(to_i915(obj->base.dev)) ?
  3378. I915_CACHE_WT : I915_CACHE_NONE);
  3379. if (ret) {
  3380. vma = ERR_PTR(ret);
  3381. goto err_unpin_global;
  3382. }
  3383. /* As the user may map the buffer once pinned in the display plane
  3384. * (e.g. libkms for the bootup splash), we have to ensure that we
  3385. * always use map_and_fenceable for all scanout buffers. However,
  3386. * it may simply be too big to fit into mappable, in which case
  3387. * put it anyway and hope that userspace can cope (but always first
  3388. * try to preserve the existing ABI).
  3389. */
  3390. vma = ERR_PTR(-ENOSPC);
  3391. if (!view || view->type == I915_GGTT_VIEW_NORMAL)
  3392. vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment,
  3393. PIN_MAPPABLE | PIN_NONBLOCK);
  3394. if (IS_ERR(vma)) {
  3395. struct drm_i915_private *i915 = to_i915(obj->base.dev);
  3396. unsigned int flags;
  3397. /* Valleyview is definitely limited to scanning out the first
  3398. * 512MiB. Lets presume this behaviour was inherited from the
  3399. * g4x display engine and that all earlier gen are similarly
  3400. * limited. Testing suggests that it is a little more
  3401. * complicated than this. For example, Cherryview appears quite
  3402. * happy to scanout from anywhere within its global aperture.
  3403. */
  3404. flags = 0;
  3405. if (HAS_GMCH_DISPLAY(i915))
  3406. flags = PIN_MAPPABLE;
  3407. vma = i915_gem_object_ggtt_pin(obj, view, 0, alignment, flags);
  3408. }
  3409. if (IS_ERR(vma))
  3410. goto err_unpin_global;
  3411. vma->display_alignment = max_t(u64, vma->display_alignment, alignment);
  3412. /* Treat this as an end-of-frame, like intel_user_framebuffer_dirty() */
  3413. __i915_gem_object_flush_for_display(obj);
  3414. intel_fb_obj_flush(obj, ORIGIN_DIRTYFB);
  3415. /* It should now be out of any other write domains, and we can update
  3416. * the domain values for our changes.
  3417. */
  3418. obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
  3419. return vma;
  3420. err_unpin_global:
  3421. obj->pin_global--;
  3422. return vma;
  3423. }
  3424. void
  3425. i915_gem_object_unpin_from_display_plane(struct i915_vma *vma)
  3426. {
  3427. lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
  3428. if (WARN_ON(vma->obj->pin_global == 0))
  3429. return;
  3430. if (--vma->obj->pin_global == 0)
  3431. vma->display_alignment = I915_GTT_MIN_ALIGNMENT;
  3432. /* Bump the LRU to try and avoid premature eviction whilst flipping */
  3433. i915_gem_object_bump_inactive_ggtt(vma->obj);
  3434. i915_vma_unpin(vma);
  3435. }
  3436. /**
  3437. * Moves a single object to the CPU read, and possibly write domain.
  3438. * @obj: object to act on
  3439. * @write: requesting write or read-only access
  3440. *
  3441. * This function returns when the move is complete, including waiting on
  3442. * flushes to occur.
  3443. */
  3444. int
  3445. i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
  3446. {
  3447. int ret;
  3448. lockdep_assert_held(&obj->base.dev->struct_mutex);
  3449. ret = i915_gem_object_wait(obj,
  3450. I915_WAIT_INTERRUPTIBLE |
  3451. I915_WAIT_LOCKED |
  3452. (write ? I915_WAIT_ALL : 0),
  3453. MAX_SCHEDULE_TIMEOUT,
  3454. NULL);
  3455. if (ret)
  3456. return ret;
  3457. flush_write_domain(obj, ~I915_GEM_DOMAIN_CPU);
  3458. /* Flush the CPU cache if it's still invalid. */
  3459. if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) {
  3460. i915_gem_clflush_object(obj, I915_CLFLUSH_SYNC);
  3461. obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
  3462. }
  3463. /* It should now be out of any other write domains, and we can update
  3464. * the domain values for our changes.
  3465. */
  3466. GEM_BUG_ON(obj->base.write_domain & ~I915_GEM_DOMAIN_CPU);
  3467. /* If we're writing through the CPU, then the GPU read domains will
  3468. * need to be invalidated at next use.
  3469. */
  3470. if (write)
  3471. __start_cpu_write(obj);
  3472. return 0;
  3473. }
  3474. /* Throttle our rendering by waiting until the ring has completed our requests
  3475. * emitted over 20 msec ago.
  3476. *
  3477. * Note that if we were to use the current jiffies each time around the loop,
  3478. * we wouldn't escape the function with any frames outstanding if the time to
  3479. * render a frame was over 20ms.
  3480. *
  3481. * This should get us reasonable parallelism between CPU and GPU but also
  3482. * relatively low latency when blocking on a particular request to finish.
  3483. */
  3484. static int
  3485. i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
  3486. {
  3487. struct drm_i915_private *dev_priv = to_i915(dev);
  3488. struct drm_i915_file_private *file_priv = file->driver_priv;
  3489. unsigned long recent_enough = jiffies - DRM_I915_THROTTLE_JIFFIES;
  3490. struct drm_i915_gem_request *request, *target = NULL;
  3491. long ret;
  3492. /* ABI: return -EIO if already wedged */
  3493. if (i915_terminally_wedged(&dev_priv->gpu_error))
  3494. return -EIO;
  3495. spin_lock(&file_priv->mm.lock);
  3496. list_for_each_entry(request, &file_priv->mm.request_list, client_link) {
  3497. if (time_after_eq(request->emitted_jiffies, recent_enough))
  3498. break;
  3499. if (target) {
  3500. list_del(&target->client_link);
  3501. target->file_priv = NULL;
  3502. }
  3503. target = request;
  3504. }
  3505. if (target)
  3506. i915_gem_request_get(target);
  3507. spin_unlock(&file_priv->mm.lock);
  3508. if (target == NULL)
  3509. return 0;
  3510. ret = i915_wait_request(target,
  3511. I915_WAIT_INTERRUPTIBLE,
  3512. MAX_SCHEDULE_TIMEOUT);
  3513. i915_gem_request_put(target);
  3514. return ret < 0 ? ret : 0;
  3515. }
  3516. struct i915_vma *
  3517. i915_gem_object_ggtt_pin(struct drm_i915_gem_object *obj,
  3518. const struct i915_ggtt_view *view,
  3519. u64 size,
  3520. u64 alignment,
  3521. u64 flags)
  3522. {
  3523. struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  3524. struct i915_address_space *vm = &dev_priv->ggtt.base;
  3525. struct i915_vma *vma;
  3526. int ret;
  3527. lockdep_assert_held(&obj->base.dev->struct_mutex);
  3528. if (!view && flags & PIN_MAPPABLE) {
  3529. /* If the required space is larger than the available
  3530. * aperture, we will not able to find a slot for the
  3531. * object and unbinding the object now will be in
  3532. * vain. Worse, doing so may cause us to ping-pong
  3533. * the object in and out of the Global GTT and
  3534. * waste a lot of cycles under the mutex.
  3535. */
  3536. if (obj->base.size > dev_priv->ggtt.mappable_end)
  3537. return ERR_PTR(-E2BIG);
  3538. /* If NONBLOCK is set the caller is optimistically
  3539. * trying to cache the full object within the mappable
  3540. * aperture, and *must* have a fallback in place for
  3541. * situations where we cannot bind the object. We
  3542. * can be a little more lax here and use the fallback
  3543. * more often to avoid costly migrations of ourselves
  3544. * and other objects within the aperture.
  3545. *
  3546. * Half-the-aperture is used as a simple heuristic.
  3547. * More interesting would to do search for a free
  3548. * block prior to making the commitment to unbind.
  3549. * That caters for the self-harm case, and with a
  3550. * little more heuristics (e.g. NOFAULT, NOEVICT)
  3551. * we could try to minimise harm to others.
  3552. */
  3553. if (flags & PIN_NONBLOCK &&
  3554. obj->base.size > dev_priv->ggtt.mappable_end / 2)
  3555. return ERR_PTR(-ENOSPC);
  3556. }
  3557. vma = i915_vma_instance(obj, vm, view);
  3558. if (unlikely(IS_ERR(vma)))
  3559. return vma;
  3560. if (i915_vma_misplaced(vma, size, alignment, flags)) {
  3561. if (flags & PIN_NONBLOCK) {
  3562. if (i915_vma_is_pinned(vma) || i915_vma_is_active(vma))
  3563. return ERR_PTR(-ENOSPC);
  3564. if (flags & PIN_MAPPABLE &&
  3565. vma->fence_size > dev_priv->ggtt.mappable_end / 2)
  3566. return ERR_PTR(-ENOSPC);
  3567. }
  3568. WARN(i915_vma_is_pinned(vma),
  3569. "bo is already pinned in ggtt with incorrect alignment:"
  3570. " offset=%08x, req.alignment=%llx,"
  3571. " req.map_and_fenceable=%d, vma->map_and_fenceable=%d\n",
  3572. i915_ggtt_offset(vma), alignment,
  3573. !!(flags & PIN_MAPPABLE),
  3574. i915_vma_is_map_and_fenceable(vma));
  3575. ret = i915_vma_unbind(vma);
  3576. if (ret)
  3577. return ERR_PTR(ret);
  3578. }
  3579. ret = i915_vma_pin(vma, size, alignment, flags | PIN_GLOBAL);
  3580. if (ret)
  3581. return ERR_PTR(ret);
  3582. return vma;
  3583. }
  3584. static __always_inline unsigned int __busy_read_flag(unsigned int id)
  3585. {
  3586. /* Note that we could alias engines in the execbuf API, but
  3587. * that would be very unwise as it prevents userspace from
  3588. * fine control over engine selection. Ahem.
  3589. *
  3590. * This should be something like EXEC_MAX_ENGINE instead of
  3591. * I915_NUM_ENGINES.
  3592. */
  3593. BUILD_BUG_ON(I915_NUM_ENGINES > 16);
  3594. return 0x10000 << id;
  3595. }
  3596. static __always_inline unsigned int __busy_write_id(unsigned int id)
  3597. {
  3598. /* The uABI guarantees an active writer is also amongst the read
  3599. * engines. This would be true if we accessed the activity tracking
  3600. * under the lock, but as we perform the lookup of the object and
  3601. * its activity locklessly we can not guarantee that the last_write
  3602. * being active implies that we have set the same engine flag from
  3603. * last_read - hence we always set both read and write busy for
  3604. * last_write.
  3605. */
  3606. return id | __busy_read_flag(id);
  3607. }
  3608. static __always_inline unsigned int
  3609. __busy_set_if_active(const struct dma_fence *fence,
  3610. unsigned int (*flag)(unsigned int id))
  3611. {
  3612. struct drm_i915_gem_request *rq;
  3613. /* We have to check the current hw status of the fence as the uABI
  3614. * guarantees forward progress. We could rely on the idle worker
  3615. * to eventually flush us, but to minimise latency just ask the
  3616. * hardware.
  3617. *
  3618. * Note we only report on the status of native fences.
  3619. */
  3620. if (!dma_fence_is_i915(fence))
  3621. return 0;
  3622. /* opencode to_request() in order to avoid const warnings */
  3623. rq = container_of(fence, struct drm_i915_gem_request, fence);
  3624. if (i915_gem_request_completed(rq))
  3625. return 0;
  3626. return flag(rq->engine->uabi_id);
  3627. }
  3628. static __always_inline unsigned int
  3629. busy_check_reader(const struct dma_fence *fence)
  3630. {
  3631. return __busy_set_if_active(fence, __busy_read_flag);
  3632. }
  3633. static __always_inline unsigned int
  3634. busy_check_writer(const struct dma_fence *fence)
  3635. {
  3636. if (!fence)
  3637. return 0;
  3638. return __busy_set_if_active(fence, __busy_write_id);
  3639. }
  3640. int
  3641. i915_gem_busy_ioctl(struct drm_device *dev, void *data,
  3642. struct drm_file *file)
  3643. {
  3644. struct drm_i915_gem_busy *args = data;
  3645. struct drm_i915_gem_object *obj;
  3646. struct reservation_object_list *list;
  3647. unsigned int seq;
  3648. int err;
  3649. err = -ENOENT;
  3650. rcu_read_lock();
  3651. obj = i915_gem_object_lookup_rcu(file, args->handle);
  3652. if (!obj)
  3653. goto out;
  3654. /* A discrepancy here is that we do not report the status of
  3655. * non-i915 fences, i.e. even though we may report the object as idle,
  3656. * a call to set-domain may still stall waiting for foreign rendering.
  3657. * This also means that wait-ioctl may report an object as busy,
  3658. * where busy-ioctl considers it idle.
  3659. *
  3660. * We trade the ability to warn of foreign fences to report on which
  3661. * i915 engines are active for the object.
  3662. *
  3663. * Alternatively, we can trade that extra information on read/write
  3664. * activity with
  3665. * args->busy =
  3666. * !reservation_object_test_signaled_rcu(obj->resv, true);
  3667. * to report the overall busyness. This is what the wait-ioctl does.
  3668. *
  3669. */
  3670. retry:
  3671. seq = raw_read_seqcount(&obj->resv->seq);
  3672. /* Translate the exclusive fence to the READ *and* WRITE engine */
  3673. args->busy = busy_check_writer(rcu_dereference(obj->resv->fence_excl));
  3674. /* Translate shared fences to READ set of engines */
  3675. list = rcu_dereference(obj->resv->fence);
  3676. if (list) {
  3677. unsigned int shared_count = list->shared_count, i;
  3678. for (i = 0; i < shared_count; ++i) {
  3679. struct dma_fence *fence =
  3680. rcu_dereference(list->shared[i]);
  3681. args->busy |= busy_check_reader(fence);
  3682. }
  3683. }
  3684. if (args->busy && read_seqcount_retry(&obj->resv->seq, seq))
  3685. goto retry;
  3686. err = 0;
  3687. out:
  3688. rcu_read_unlock();
  3689. return err;
  3690. }
  3691. int
  3692. i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
  3693. struct drm_file *file_priv)
  3694. {
  3695. return i915_gem_ring_throttle(dev, file_priv);
  3696. }
  3697. int
  3698. i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
  3699. struct drm_file *file_priv)
  3700. {
  3701. struct drm_i915_private *dev_priv = to_i915(dev);
  3702. struct drm_i915_gem_madvise *args = data;
  3703. struct drm_i915_gem_object *obj;
  3704. int err;
  3705. switch (args->madv) {
  3706. case I915_MADV_DONTNEED:
  3707. case I915_MADV_WILLNEED:
  3708. break;
  3709. default:
  3710. return -EINVAL;
  3711. }
  3712. obj = i915_gem_object_lookup(file_priv, args->handle);
  3713. if (!obj)
  3714. return -ENOENT;
  3715. err = mutex_lock_interruptible(&obj->mm.lock);
  3716. if (err)
  3717. goto out;
  3718. if (i915_gem_object_has_pages(obj) &&
  3719. i915_gem_object_is_tiled(obj) &&
  3720. dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
  3721. if (obj->mm.madv == I915_MADV_WILLNEED) {
  3722. GEM_BUG_ON(!obj->mm.quirked);
  3723. __i915_gem_object_unpin_pages(obj);
  3724. obj->mm.quirked = false;
  3725. }
  3726. if (args->madv == I915_MADV_WILLNEED) {
  3727. GEM_BUG_ON(obj->mm.quirked);
  3728. __i915_gem_object_pin_pages(obj);
  3729. obj->mm.quirked = true;
  3730. }
  3731. }
  3732. if (obj->mm.madv != __I915_MADV_PURGED)
  3733. obj->mm.madv = args->madv;
  3734. /* if the object is no longer attached, discard its backing storage */
  3735. if (obj->mm.madv == I915_MADV_DONTNEED &&
  3736. !i915_gem_object_has_pages(obj))
  3737. i915_gem_object_truncate(obj);
  3738. args->retained = obj->mm.madv != __I915_MADV_PURGED;
  3739. mutex_unlock(&obj->mm.lock);
  3740. out:
  3741. i915_gem_object_put(obj);
  3742. return err;
  3743. }
  3744. static void
  3745. frontbuffer_retire(struct i915_gem_active *active,
  3746. struct drm_i915_gem_request *request)
  3747. {
  3748. struct drm_i915_gem_object *obj =
  3749. container_of(active, typeof(*obj), frontbuffer_write);
  3750. intel_fb_obj_flush(obj, ORIGIN_CS);
  3751. }
  3752. void i915_gem_object_init(struct drm_i915_gem_object *obj,
  3753. const struct drm_i915_gem_object_ops *ops)
  3754. {
  3755. mutex_init(&obj->mm.lock);
  3756. INIT_LIST_HEAD(&obj->vma_list);
  3757. INIT_LIST_HEAD(&obj->lut_list);
  3758. INIT_LIST_HEAD(&obj->batch_pool_link);
  3759. obj->ops = ops;
  3760. reservation_object_init(&obj->__builtin_resv);
  3761. obj->resv = &obj->__builtin_resv;
  3762. obj->frontbuffer_ggtt_origin = ORIGIN_GTT;
  3763. init_request_active(&obj->frontbuffer_write, frontbuffer_retire);
  3764. obj->mm.madv = I915_MADV_WILLNEED;
  3765. INIT_RADIX_TREE(&obj->mm.get_page.radix, GFP_KERNEL | __GFP_NOWARN);
  3766. mutex_init(&obj->mm.get_page.lock);
  3767. i915_gem_info_add_obj(to_i915(obj->base.dev), obj->base.size);
  3768. }
  3769. static const struct drm_i915_gem_object_ops i915_gem_object_ops = {
  3770. .flags = I915_GEM_OBJECT_HAS_STRUCT_PAGE |
  3771. I915_GEM_OBJECT_IS_SHRINKABLE,
  3772. .get_pages = i915_gem_object_get_pages_gtt,
  3773. .put_pages = i915_gem_object_put_pages_gtt,
  3774. .pwrite = i915_gem_object_pwrite_gtt,
  3775. };
  3776. static int i915_gem_object_create_shmem(struct drm_device *dev,
  3777. struct drm_gem_object *obj,
  3778. size_t size)
  3779. {
  3780. struct drm_i915_private *i915 = to_i915(dev);
  3781. unsigned long flags = VM_NORESERVE;
  3782. struct file *filp;
  3783. drm_gem_private_object_init(dev, obj, size);
  3784. if (i915->mm.gemfs)
  3785. filp = shmem_file_setup_with_mnt(i915->mm.gemfs, "i915", size,
  3786. flags);
  3787. else
  3788. filp = shmem_file_setup("i915", size, flags);
  3789. if (IS_ERR(filp))
  3790. return PTR_ERR(filp);
  3791. obj->filp = filp;
  3792. return 0;
  3793. }
  3794. struct drm_i915_gem_object *
  3795. i915_gem_object_create(struct drm_i915_private *dev_priv, u64 size)
  3796. {
  3797. struct drm_i915_gem_object *obj;
  3798. struct address_space *mapping;
  3799. unsigned int cache_level;
  3800. gfp_t mask;
  3801. int ret;
  3802. /* There is a prevalence of the assumption that we fit the object's
  3803. * page count inside a 32bit _signed_ variable. Let's document this and
  3804. * catch if we ever need to fix it. In the meantime, if you do spot
  3805. * such a local variable, please consider fixing!
  3806. */
  3807. if (size >> PAGE_SHIFT > INT_MAX)
  3808. return ERR_PTR(-E2BIG);
  3809. if (overflows_type(size, obj->base.size))
  3810. return ERR_PTR(-E2BIG);
  3811. obj = i915_gem_object_alloc(dev_priv);
  3812. if (obj == NULL)
  3813. return ERR_PTR(-ENOMEM);
  3814. ret = i915_gem_object_create_shmem(&dev_priv->drm, &obj->base, size);
  3815. if (ret)
  3816. goto fail;
  3817. mask = GFP_HIGHUSER | __GFP_RECLAIMABLE;
  3818. if (IS_I965GM(dev_priv) || IS_I965G(dev_priv)) {
  3819. /* 965gm cannot relocate objects above 4GiB. */
  3820. mask &= ~__GFP_HIGHMEM;
  3821. mask |= __GFP_DMA32;
  3822. }
  3823. mapping = obj->base.filp->f_mapping;
  3824. mapping_set_gfp_mask(mapping, mask);
  3825. GEM_BUG_ON(!(mapping_gfp_mask(mapping) & __GFP_RECLAIM));
  3826. i915_gem_object_init(obj, &i915_gem_object_ops);
  3827. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  3828. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  3829. if (HAS_LLC(dev_priv))
  3830. /* On some devices, we can have the GPU use the LLC (the CPU
  3831. * cache) for about a 10% performance improvement
  3832. * compared to uncached. Graphics requests other than
  3833. * display scanout are coherent with the CPU in
  3834. * accessing this cache. This means in this mode we
  3835. * don't need to clflush on the CPU side, and on the
  3836. * GPU side we only need to flush internal caches to
  3837. * get data visible to the CPU.
  3838. *
  3839. * However, we maintain the display planes as UC, and so
  3840. * need to rebind when first used as such.
  3841. */
  3842. cache_level = I915_CACHE_LLC;
  3843. else
  3844. cache_level = I915_CACHE_NONE;
  3845. i915_gem_object_set_cache_coherency(obj, cache_level);
  3846. trace_i915_gem_object_create(obj);
  3847. return obj;
  3848. fail:
  3849. i915_gem_object_free(obj);
  3850. return ERR_PTR(ret);
  3851. }
  3852. static bool discard_backing_storage(struct drm_i915_gem_object *obj)
  3853. {
  3854. /* If we are the last user of the backing storage (be it shmemfs
  3855. * pages or stolen etc), we know that the pages are going to be
  3856. * immediately released. In this case, we can then skip copying
  3857. * back the contents from the GPU.
  3858. */
  3859. if (obj->mm.madv != I915_MADV_WILLNEED)
  3860. return false;
  3861. if (obj->base.filp == NULL)
  3862. return true;
  3863. /* At first glance, this looks racy, but then again so would be
  3864. * userspace racing mmap against close. However, the first external
  3865. * reference to the filp can only be obtained through the
  3866. * i915_gem_mmap_ioctl() which safeguards us against the user
  3867. * acquiring such a reference whilst we are in the middle of
  3868. * freeing the object.
  3869. */
  3870. return atomic_long_read(&obj->base.filp->f_count) == 1;
  3871. }
  3872. static void __i915_gem_free_objects(struct drm_i915_private *i915,
  3873. struct llist_node *freed)
  3874. {
  3875. struct drm_i915_gem_object *obj, *on;
  3876. intel_runtime_pm_get(i915);
  3877. llist_for_each_entry_safe(obj, on, freed, freed) {
  3878. struct i915_vma *vma, *vn;
  3879. trace_i915_gem_object_destroy(obj);
  3880. mutex_lock(&i915->drm.struct_mutex);
  3881. GEM_BUG_ON(i915_gem_object_is_active(obj));
  3882. list_for_each_entry_safe(vma, vn,
  3883. &obj->vma_list, obj_link) {
  3884. GEM_BUG_ON(i915_vma_is_active(vma));
  3885. vma->flags &= ~I915_VMA_PIN_MASK;
  3886. i915_vma_close(vma);
  3887. }
  3888. GEM_BUG_ON(!list_empty(&obj->vma_list));
  3889. GEM_BUG_ON(!RB_EMPTY_ROOT(&obj->vma_tree));
  3890. /* This serializes freeing with the shrinker. Since the free
  3891. * is delayed, first by RCU then by the workqueue, we want the
  3892. * shrinker to be able to free pages of unreferenced objects,
  3893. * or else we may oom whilst there are plenty of deferred
  3894. * freed objects.
  3895. */
  3896. if (i915_gem_object_has_pages(obj)) {
  3897. spin_lock(&i915->mm.obj_lock);
  3898. list_del_init(&obj->mm.link);
  3899. spin_unlock(&i915->mm.obj_lock);
  3900. }
  3901. mutex_unlock(&i915->drm.struct_mutex);
  3902. GEM_BUG_ON(obj->bind_count);
  3903. GEM_BUG_ON(obj->userfault_count);
  3904. GEM_BUG_ON(atomic_read(&obj->frontbuffer_bits));
  3905. GEM_BUG_ON(!list_empty(&obj->lut_list));
  3906. if (obj->ops->release)
  3907. obj->ops->release(obj);
  3908. if (WARN_ON(i915_gem_object_has_pinned_pages(obj)))
  3909. atomic_set(&obj->mm.pages_pin_count, 0);
  3910. __i915_gem_object_put_pages(obj, I915_MM_NORMAL);
  3911. GEM_BUG_ON(i915_gem_object_has_pages(obj));
  3912. if (obj->base.import_attach)
  3913. drm_prime_gem_destroy(&obj->base, NULL);
  3914. reservation_object_fini(&obj->__builtin_resv);
  3915. drm_gem_object_release(&obj->base);
  3916. i915_gem_info_remove_obj(i915, obj->base.size);
  3917. kfree(obj->bit_17);
  3918. i915_gem_object_free(obj);
  3919. if (on)
  3920. cond_resched();
  3921. }
  3922. intel_runtime_pm_put(i915);
  3923. }
  3924. static void i915_gem_flush_free_objects(struct drm_i915_private *i915)
  3925. {
  3926. struct llist_node *freed;
  3927. /* Free the oldest, most stale object to keep the free_list short */
  3928. freed = NULL;
  3929. if (!llist_empty(&i915->mm.free_list)) { /* quick test for hotpath */
  3930. /* Only one consumer of llist_del_first() allowed */
  3931. spin_lock(&i915->mm.free_lock);
  3932. freed = llist_del_first(&i915->mm.free_list);
  3933. spin_unlock(&i915->mm.free_lock);
  3934. }
  3935. if (unlikely(freed)) {
  3936. freed->next = NULL;
  3937. __i915_gem_free_objects(i915, freed);
  3938. }
  3939. }
  3940. static void __i915_gem_free_work(struct work_struct *work)
  3941. {
  3942. struct drm_i915_private *i915 =
  3943. container_of(work, struct drm_i915_private, mm.free_work);
  3944. struct llist_node *freed;
  3945. /* All file-owned VMA should have been released by this point through
  3946. * i915_gem_close_object(), or earlier by i915_gem_context_close().
  3947. * However, the object may also be bound into the global GTT (e.g.
  3948. * older GPUs without per-process support, or for direct access through
  3949. * the GTT either for the user or for scanout). Those VMA still need to
  3950. * unbound now.
  3951. */
  3952. spin_lock(&i915->mm.free_lock);
  3953. while ((freed = llist_del_all(&i915->mm.free_list))) {
  3954. spin_unlock(&i915->mm.free_lock);
  3955. __i915_gem_free_objects(i915, freed);
  3956. if (need_resched())
  3957. return;
  3958. spin_lock(&i915->mm.free_lock);
  3959. }
  3960. spin_unlock(&i915->mm.free_lock);
  3961. }
  3962. static void __i915_gem_free_object_rcu(struct rcu_head *head)
  3963. {
  3964. struct drm_i915_gem_object *obj =
  3965. container_of(head, typeof(*obj), rcu);
  3966. struct drm_i915_private *i915 = to_i915(obj->base.dev);
  3967. /* We can't simply use call_rcu() from i915_gem_free_object()
  3968. * as we need to block whilst unbinding, and the call_rcu
  3969. * task may be called from softirq context. So we take a
  3970. * detour through a worker.
  3971. */
  3972. if (llist_add(&obj->freed, &i915->mm.free_list))
  3973. schedule_work(&i915->mm.free_work);
  3974. }
  3975. void i915_gem_free_object(struct drm_gem_object *gem_obj)
  3976. {
  3977. struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
  3978. if (obj->mm.quirked)
  3979. __i915_gem_object_unpin_pages(obj);
  3980. if (discard_backing_storage(obj))
  3981. obj->mm.madv = I915_MADV_DONTNEED;
  3982. /* Before we free the object, make sure any pure RCU-only
  3983. * read-side critical sections are complete, e.g.
  3984. * i915_gem_busy_ioctl(). For the corresponding synchronized
  3985. * lookup see i915_gem_object_lookup_rcu().
  3986. */
  3987. call_rcu(&obj->rcu, __i915_gem_free_object_rcu);
  3988. }
  3989. void __i915_gem_object_release_unless_active(struct drm_i915_gem_object *obj)
  3990. {
  3991. lockdep_assert_held(&obj->base.dev->struct_mutex);
  3992. if (!i915_gem_object_has_active_reference(obj) &&
  3993. i915_gem_object_is_active(obj))
  3994. i915_gem_object_set_active_reference(obj);
  3995. else
  3996. i915_gem_object_put(obj);
  3997. }
  3998. static void assert_kernel_context_is_current(struct drm_i915_private *i915)
  3999. {
  4000. struct i915_gem_context *kernel_context = i915->kernel_context;
  4001. struct intel_engine_cs *engine;
  4002. enum intel_engine_id id;
  4003. for_each_engine(engine, i915, id) {
  4004. GEM_BUG_ON(__i915_gem_active_peek(&engine->timeline->last_request));
  4005. GEM_BUG_ON(engine->last_retired_context != kernel_context);
  4006. }
  4007. }
  4008. void i915_gem_sanitize(struct drm_i915_private *i915)
  4009. {
  4010. if (i915_terminally_wedged(&i915->gpu_error)) {
  4011. mutex_lock(&i915->drm.struct_mutex);
  4012. i915_gem_unset_wedged(i915);
  4013. mutex_unlock(&i915->drm.struct_mutex);
  4014. }
  4015. /*
  4016. * If we inherit context state from the BIOS or earlier occupants
  4017. * of the GPU, the GPU may be in an inconsistent state when we
  4018. * try to take over. The only way to remove the earlier state
  4019. * is by resetting. However, resetting on earlier gen is tricky as
  4020. * it may impact the display and we are uncertain about the stability
  4021. * of the reset, so this could be applied to even earlier gen.
  4022. */
  4023. if (INTEL_GEN(i915) >= 5) {
  4024. int reset = intel_gpu_reset(i915, ALL_ENGINES);
  4025. WARN_ON(reset && reset != -ENODEV);
  4026. }
  4027. }
  4028. int i915_gem_suspend(struct drm_i915_private *dev_priv)
  4029. {
  4030. struct drm_device *dev = &dev_priv->drm;
  4031. int ret;
  4032. intel_runtime_pm_get(dev_priv);
  4033. intel_suspend_gt_powersave(dev_priv);
  4034. mutex_lock(&dev->struct_mutex);
  4035. /* We have to flush all the executing contexts to main memory so
  4036. * that they can saved in the hibernation image. To ensure the last
  4037. * context image is coherent, we have to switch away from it. That
  4038. * leaves the dev_priv->kernel_context still active when
  4039. * we actually suspend, and its image in memory may not match the GPU
  4040. * state. Fortunately, the kernel_context is disposable and we do
  4041. * not rely on its state.
  4042. */
  4043. if (!i915_terminally_wedged(&dev_priv->gpu_error)) {
  4044. ret = i915_gem_switch_to_kernel_context(dev_priv);
  4045. if (ret)
  4046. goto err_unlock;
  4047. ret = i915_gem_wait_for_idle(dev_priv,
  4048. I915_WAIT_INTERRUPTIBLE |
  4049. I915_WAIT_LOCKED);
  4050. if (ret && ret != -EIO)
  4051. goto err_unlock;
  4052. assert_kernel_context_is_current(dev_priv);
  4053. }
  4054. i915_gem_contexts_lost(dev_priv);
  4055. mutex_unlock(&dev->struct_mutex);
  4056. intel_guc_suspend(dev_priv);
  4057. cancel_delayed_work_sync(&dev_priv->gpu_error.hangcheck_work);
  4058. cancel_delayed_work_sync(&dev_priv->gt.retire_work);
  4059. /* As the idle_work is rearming if it detects a race, play safe and
  4060. * repeat the flush until it is definitely idle.
  4061. */
  4062. drain_delayed_work(&dev_priv->gt.idle_work);
  4063. /* Assert that we sucessfully flushed all the work and
  4064. * reset the GPU back to its idle, low power state.
  4065. */
  4066. WARN_ON(dev_priv->gt.awake);
  4067. if (WARN_ON(!intel_engines_are_idle(dev_priv)))
  4068. i915_gem_set_wedged(dev_priv); /* no hope, discard everything */
  4069. /*
  4070. * Neither the BIOS, ourselves or any other kernel
  4071. * expects the system to be in execlists mode on startup,
  4072. * so we need to reset the GPU back to legacy mode. And the only
  4073. * known way to disable logical contexts is through a GPU reset.
  4074. *
  4075. * So in order to leave the system in a known default configuration,
  4076. * always reset the GPU upon unload and suspend. Afterwards we then
  4077. * clean up the GEM state tracking, flushing off the requests and
  4078. * leaving the system in a known idle state.
  4079. *
  4080. * Note that is of the upmost importance that the GPU is idle and
  4081. * all stray writes are flushed *before* we dismantle the backing
  4082. * storage for the pinned objects.
  4083. *
  4084. * However, since we are uncertain that resetting the GPU on older
  4085. * machines is a good idea, we don't - just in case it leaves the
  4086. * machine in an unusable condition.
  4087. */
  4088. i915_gem_sanitize(dev_priv);
  4089. intel_runtime_pm_put(dev_priv);
  4090. return 0;
  4091. err_unlock:
  4092. mutex_unlock(&dev->struct_mutex);
  4093. intel_runtime_pm_put(dev_priv);
  4094. return ret;
  4095. }
  4096. void i915_gem_resume(struct drm_i915_private *i915)
  4097. {
  4098. WARN_ON(i915->gt.awake);
  4099. mutex_lock(&i915->drm.struct_mutex);
  4100. intel_uncore_forcewake_get(i915, FORCEWAKE_ALL);
  4101. i915_gem_restore_gtt_mappings(i915);
  4102. i915_gem_restore_fences(i915);
  4103. /*
  4104. * As we didn't flush the kernel context before suspend, we cannot
  4105. * guarantee that the context image is complete. So let's just reset
  4106. * it and start again.
  4107. */
  4108. i915->gt.resume(i915);
  4109. if (i915_gem_init_hw(i915))
  4110. goto err_wedged;
  4111. intel_guc_resume(i915);
  4112. /* Always reload a context for powersaving. */
  4113. if (i915_gem_switch_to_kernel_context(i915))
  4114. goto err_wedged;
  4115. out_unlock:
  4116. intel_uncore_forcewake_put(i915, FORCEWAKE_ALL);
  4117. mutex_unlock(&i915->drm.struct_mutex);
  4118. return;
  4119. err_wedged:
  4120. if (!i915_terminally_wedged(&i915->gpu_error)) {
  4121. DRM_ERROR("failed to re-initialize GPU, declaring wedged!\n");
  4122. i915_gem_set_wedged(i915);
  4123. }
  4124. goto out_unlock;
  4125. }
  4126. void i915_gem_init_swizzling(struct drm_i915_private *dev_priv)
  4127. {
  4128. if (INTEL_GEN(dev_priv) < 5 ||
  4129. dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE)
  4130. return;
  4131. I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
  4132. DISP_TILE_SURFACE_SWIZZLING);
  4133. if (IS_GEN5(dev_priv))
  4134. return;
  4135. I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL);
  4136. if (IS_GEN6(dev_priv))
  4137. I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB));
  4138. else if (IS_GEN7(dev_priv))
  4139. I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB));
  4140. else if (IS_GEN8(dev_priv))
  4141. I915_WRITE(GAMTARBMODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_BDW));
  4142. else
  4143. BUG();
  4144. }
  4145. static void init_unused_ring(struct drm_i915_private *dev_priv, u32 base)
  4146. {
  4147. I915_WRITE(RING_CTL(base), 0);
  4148. I915_WRITE(RING_HEAD(base), 0);
  4149. I915_WRITE(RING_TAIL(base), 0);
  4150. I915_WRITE(RING_START(base), 0);
  4151. }
  4152. static void init_unused_rings(struct drm_i915_private *dev_priv)
  4153. {
  4154. if (IS_I830(dev_priv)) {
  4155. init_unused_ring(dev_priv, PRB1_BASE);
  4156. init_unused_ring(dev_priv, SRB0_BASE);
  4157. init_unused_ring(dev_priv, SRB1_BASE);
  4158. init_unused_ring(dev_priv, SRB2_BASE);
  4159. init_unused_ring(dev_priv, SRB3_BASE);
  4160. } else if (IS_GEN2(dev_priv)) {
  4161. init_unused_ring(dev_priv, SRB0_BASE);
  4162. init_unused_ring(dev_priv, SRB1_BASE);
  4163. } else if (IS_GEN3(dev_priv)) {
  4164. init_unused_ring(dev_priv, PRB1_BASE);
  4165. init_unused_ring(dev_priv, PRB2_BASE);
  4166. }
  4167. }
  4168. static int __i915_gem_restart_engines(void *data)
  4169. {
  4170. struct drm_i915_private *i915 = data;
  4171. struct intel_engine_cs *engine;
  4172. enum intel_engine_id id;
  4173. int err;
  4174. for_each_engine(engine, i915, id) {
  4175. err = engine->init_hw(engine);
  4176. if (err)
  4177. return err;
  4178. }
  4179. return 0;
  4180. }
  4181. int i915_gem_init_hw(struct drm_i915_private *dev_priv)
  4182. {
  4183. int ret;
  4184. dev_priv->gt.last_init_time = ktime_get();
  4185. /* Double layer security blanket, see i915_gem_init() */
  4186. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  4187. if (HAS_EDRAM(dev_priv) && INTEL_GEN(dev_priv) < 9)
  4188. I915_WRITE(HSW_IDICR, I915_READ(HSW_IDICR) | IDIHASHMSK(0xf));
  4189. if (IS_HASWELL(dev_priv))
  4190. I915_WRITE(MI_PREDICATE_RESULT_2, IS_HSW_GT3(dev_priv) ?
  4191. LOWER_SLICE_ENABLED : LOWER_SLICE_DISABLED);
  4192. if (HAS_PCH_NOP(dev_priv)) {
  4193. if (IS_IVYBRIDGE(dev_priv)) {
  4194. u32 temp = I915_READ(GEN7_MSG_CTL);
  4195. temp &= ~(WAIT_FOR_PCH_FLR_ACK | WAIT_FOR_PCH_RESET_ACK);
  4196. I915_WRITE(GEN7_MSG_CTL, temp);
  4197. } else if (INTEL_GEN(dev_priv) >= 7) {
  4198. u32 temp = I915_READ(HSW_NDE_RSTWRN_OPT);
  4199. temp &= ~RESET_PCH_HANDSHAKE_ENABLE;
  4200. I915_WRITE(HSW_NDE_RSTWRN_OPT, temp);
  4201. }
  4202. }
  4203. i915_gem_init_swizzling(dev_priv);
  4204. /*
  4205. * At least 830 can leave some of the unused rings
  4206. * "active" (ie. head != tail) after resume which
  4207. * will prevent c3 entry. Makes sure all unused rings
  4208. * are totally idle.
  4209. */
  4210. init_unused_rings(dev_priv);
  4211. BUG_ON(!dev_priv->kernel_context);
  4212. if (i915_terminally_wedged(&dev_priv->gpu_error)) {
  4213. ret = -EIO;
  4214. goto out;
  4215. }
  4216. ret = i915_ppgtt_init_hw(dev_priv);
  4217. if (ret) {
  4218. DRM_ERROR("PPGTT enable HW failed %d\n", ret);
  4219. goto out;
  4220. }
  4221. /* We can't enable contexts until all firmware is loaded */
  4222. ret = intel_uc_init_hw(dev_priv);
  4223. if (ret)
  4224. goto out;
  4225. intel_mocs_init_l3cc_table(dev_priv);
  4226. /* Only when the HW is re-initialised, can we replay the requests */
  4227. ret = __i915_gem_restart_engines(dev_priv);
  4228. out:
  4229. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  4230. return ret;
  4231. }
  4232. static int __intel_engines_record_defaults(struct drm_i915_private *i915)
  4233. {
  4234. struct i915_gem_context *ctx;
  4235. struct intel_engine_cs *engine;
  4236. enum intel_engine_id id;
  4237. int err;
  4238. /*
  4239. * As we reset the gpu during very early sanitisation, the current
  4240. * register state on the GPU should reflect its defaults values.
  4241. * We load a context onto the hw (with restore-inhibit), then switch
  4242. * over to a second context to save that default register state. We
  4243. * can then prime every new context with that state so they all start
  4244. * from the same default HW values.
  4245. */
  4246. ctx = i915_gem_context_create_kernel(i915, 0);
  4247. if (IS_ERR(ctx))
  4248. return PTR_ERR(ctx);
  4249. for_each_engine(engine, i915, id) {
  4250. struct drm_i915_gem_request *rq;
  4251. rq = i915_gem_request_alloc(engine, ctx);
  4252. if (IS_ERR(rq)) {
  4253. err = PTR_ERR(rq);
  4254. goto out_ctx;
  4255. }
  4256. err = 0;
  4257. if (engine->init_context)
  4258. err = engine->init_context(rq);
  4259. __i915_add_request(rq, true);
  4260. if (err)
  4261. goto err_active;
  4262. }
  4263. err = i915_gem_switch_to_kernel_context(i915);
  4264. if (err)
  4265. goto err_active;
  4266. err = i915_gem_wait_for_idle(i915, I915_WAIT_LOCKED);
  4267. if (err)
  4268. goto err_active;
  4269. assert_kernel_context_is_current(i915);
  4270. for_each_engine(engine, i915, id) {
  4271. struct i915_vma *state;
  4272. state = ctx->engine[id].state;
  4273. if (!state)
  4274. continue;
  4275. /*
  4276. * As we will hold a reference to the logical state, it will
  4277. * not be torn down with the context, and importantly the
  4278. * object will hold onto its vma (making it possible for a
  4279. * stray GTT write to corrupt our defaults). Unmap the vma
  4280. * from the GTT to prevent such accidents and reclaim the
  4281. * space.
  4282. */
  4283. err = i915_vma_unbind(state);
  4284. if (err)
  4285. goto err_active;
  4286. err = i915_gem_object_set_to_cpu_domain(state->obj, false);
  4287. if (err)
  4288. goto err_active;
  4289. engine->default_state = i915_gem_object_get(state->obj);
  4290. }
  4291. if (IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM)) {
  4292. unsigned int found = intel_engines_has_context_isolation(i915);
  4293. /*
  4294. * Make sure that classes with multiple engine instances all
  4295. * share the same basic configuration.
  4296. */
  4297. for_each_engine(engine, i915, id) {
  4298. unsigned int bit = BIT(engine->uabi_class);
  4299. unsigned int expected = engine->default_state ? bit : 0;
  4300. if ((found & bit) != expected) {
  4301. DRM_ERROR("mismatching default context state for class %d on engine %s\n",
  4302. engine->uabi_class, engine->name);
  4303. }
  4304. }
  4305. }
  4306. out_ctx:
  4307. i915_gem_context_set_closed(ctx);
  4308. i915_gem_context_put(ctx);
  4309. return err;
  4310. err_active:
  4311. /*
  4312. * If we have to abandon now, we expect the engines to be idle
  4313. * and ready to be torn-down. First try to flush any remaining
  4314. * request, ensure we are pointing at the kernel context and
  4315. * then remove it.
  4316. */
  4317. if (WARN_ON(i915_gem_switch_to_kernel_context(i915)))
  4318. goto out_ctx;
  4319. if (WARN_ON(i915_gem_wait_for_idle(i915, I915_WAIT_LOCKED)))
  4320. goto out_ctx;
  4321. i915_gem_contexts_lost(i915);
  4322. goto out_ctx;
  4323. }
  4324. int i915_gem_init(struct drm_i915_private *dev_priv)
  4325. {
  4326. int ret;
  4327. /*
  4328. * We need to fallback to 4K pages since gvt gtt handling doesn't
  4329. * support huge page entries - we will need to check either hypervisor
  4330. * mm can support huge guest page or just do emulation in gvt.
  4331. */
  4332. if (intel_vgpu_active(dev_priv))
  4333. mkwrite_device_info(dev_priv)->page_sizes =
  4334. I915_GTT_PAGE_SIZE_4K;
  4335. dev_priv->mm.unordered_timeline = dma_fence_context_alloc(1);
  4336. if (HAS_LOGICAL_RING_CONTEXTS(dev_priv)) {
  4337. dev_priv->gt.resume = intel_lr_context_resume;
  4338. dev_priv->gt.cleanup_engine = intel_logical_ring_cleanup;
  4339. } else {
  4340. dev_priv->gt.resume = intel_legacy_submission_resume;
  4341. dev_priv->gt.cleanup_engine = intel_engine_cleanup;
  4342. }
  4343. ret = i915_gem_init_userptr(dev_priv);
  4344. if (ret)
  4345. return ret;
  4346. ret = intel_uc_init_wq(dev_priv);
  4347. if (ret)
  4348. return ret;
  4349. /* This is just a security blanket to placate dragons.
  4350. * On some systems, we very sporadically observe that the first TLBs
  4351. * used by the CS may be stale, despite us poking the TLB reset. If
  4352. * we hold the forcewake during initialisation these problems
  4353. * just magically go away.
  4354. */
  4355. mutex_lock(&dev_priv->drm.struct_mutex);
  4356. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  4357. ret = i915_gem_init_ggtt(dev_priv);
  4358. if (ret) {
  4359. GEM_BUG_ON(ret == -EIO);
  4360. goto err_unlock;
  4361. }
  4362. ret = i915_gem_contexts_init(dev_priv);
  4363. if (ret) {
  4364. GEM_BUG_ON(ret == -EIO);
  4365. goto err_ggtt;
  4366. }
  4367. ret = intel_engines_init(dev_priv);
  4368. if (ret) {
  4369. GEM_BUG_ON(ret == -EIO);
  4370. goto err_context;
  4371. }
  4372. intel_init_gt_powersave(dev_priv);
  4373. ret = intel_uc_init(dev_priv);
  4374. if (ret)
  4375. goto err_pm;
  4376. ret = i915_gem_init_hw(dev_priv);
  4377. if (ret)
  4378. goto err_uc_init;
  4379. /*
  4380. * Despite its name intel_init_clock_gating applies both display
  4381. * clock gating workarounds; GT mmio workarounds and the occasional
  4382. * GT power context workaround. Worse, sometimes it includes a context
  4383. * register workaround which we need to apply before we record the
  4384. * default HW state for all contexts.
  4385. *
  4386. * FIXME: break up the workarounds and apply them at the right time!
  4387. */
  4388. intel_init_clock_gating(dev_priv);
  4389. ret = __intel_engines_record_defaults(dev_priv);
  4390. if (ret)
  4391. goto err_init_hw;
  4392. if (i915_inject_load_failure()) {
  4393. ret = -ENODEV;
  4394. goto err_init_hw;
  4395. }
  4396. if (i915_inject_load_failure()) {
  4397. ret = -EIO;
  4398. goto err_init_hw;
  4399. }
  4400. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  4401. mutex_unlock(&dev_priv->drm.struct_mutex);
  4402. return 0;
  4403. /*
  4404. * Unwinding is complicated by that we want to handle -EIO to mean
  4405. * disable GPU submission but keep KMS alive. We want to mark the
  4406. * HW as irrevisibly wedged, but keep enough state around that the
  4407. * driver doesn't explode during runtime.
  4408. */
  4409. err_init_hw:
  4410. i915_gem_wait_for_idle(dev_priv, I915_WAIT_LOCKED);
  4411. i915_gem_contexts_lost(dev_priv);
  4412. intel_uc_fini_hw(dev_priv);
  4413. err_uc_init:
  4414. intel_uc_fini(dev_priv);
  4415. err_pm:
  4416. if (ret != -EIO) {
  4417. intel_cleanup_gt_powersave(dev_priv);
  4418. i915_gem_cleanup_engines(dev_priv);
  4419. }
  4420. err_context:
  4421. if (ret != -EIO)
  4422. i915_gem_contexts_fini(dev_priv);
  4423. err_ggtt:
  4424. err_unlock:
  4425. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  4426. mutex_unlock(&dev_priv->drm.struct_mutex);
  4427. intel_uc_fini_wq(dev_priv);
  4428. if (ret != -EIO)
  4429. i915_gem_cleanup_userptr(dev_priv);
  4430. if (ret == -EIO) {
  4431. /*
  4432. * Allow engine initialisation to fail by marking the GPU as
  4433. * wedged. But we only want to do this where the GPU is angry,
  4434. * for all other failure, such as an allocation failure, bail.
  4435. */
  4436. if (!i915_terminally_wedged(&dev_priv->gpu_error)) {
  4437. DRM_ERROR("Failed to initialize GPU, declaring it wedged\n");
  4438. i915_gem_set_wedged(dev_priv);
  4439. }
  4440. ret = 0;
  4441. }
  4442. i915_gem_drain_freed_objects(dev_priv);
  4443. return ret;
  4444. }
  4445. void i915_gem_init_mmio(struct drm_i915_private *i915)
  4446. {
  4447. i915_gem_sanitize(i915);
  4448. }
  4449. void
  4450. i915_gem_cleanup_engines(struct drm_i915_private *dev_priv)
  4451. {
  4452. struct intel_engine_cs *engine;
  4453. enum intel_engine_id id;
  4454. for_each_engine(engine, dev_priv, id)
  4455. dev_priv->gt.cleanup_engine(engine);
  4456. }
  4457. void
  4458. i915_gem_load_init_fences(struct drm_i915_private *dev_priv)
  4459. {
  4460. int i;
  4461. if (INTEL_INFO(dev_priv)->gen >= 7 && !IS_VALLEYVIEW(dev_priv) &&
  4462. !IS_CHERRYVIEW(dev_priv))
  4463. dev_priv->num_fence_regs = 32;
  4464. else if (INTEL_INFO(dev_priv)->gen >= 4 ||
  4465. IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
  4466. IS_G33(dev_priv) || IS_PINEVIEW(dev_priv))
  4467. dev_priv->num_fence_regs = 16;
  4468. else
  4469. dev_priv->num_fence_regs = 8;
  4470. if (intel_vgpu_active(dev_priv))
  4471. dev_priv->num_fence_regs =
  4472. I915_READ(vgtif_reg(avail_rs.fence_num));
  4473. /* Initialize fence registers to zero */
  4474. for (i = 0; i < dev_priv->num_fence_regs; i++) {
  4475. struct drm_i915_fence_reg *fence = &dev_priv->fence_regs[i];
  4476. fence->i915 = dev_priv;
  4477. fence->id = i;
  4478. list_add_tail(&fence->link, &dev_priv->mm.fence_list);
  4479. }
  4480. i915_gem_restore_fences(dev_priv);
  4481. i915_gem_detect_bit_6_swizzle(dev_priv);
  4482. }
  4483. static void i915_gem_init__mm(struct drm_i915_private *i915)
  4484. {
  4485. spin_lock_init(&i915->mm.object_stat_lock);
  4486. spin_lock_init(&i915->mm.obj_lock);
  4487. spin_lock_init(&i915->mm.free_lock);
  4488. init_llist_head(&i915->mm.free_list);
  4489. INIT_LIST_HEAD(&i915->mm.unbound_list);
  4490. INIT_LIST_HEAD(&i915->mm.bound_list);
  4491. INIT_LIST_HEAD(&i915->mm.fence_list);
  4492. INIT_LIST_HEAD(&i915->mm.userfault_list);
  4493. INIT_WORK(&i915->mm.free_work, __i915_gem_free_work);
  4494. }
  4495. int
  4496. i915_gem_load_init(struct drm_i915_private *dev_priv)
  4497. {
  4498. int err = -ENOMEM;
  4499. dev_priv->objects = KMEM_CACHE(drm_i915_gem_object, SLAB_HWCACHE_ALIGN);
  4500. if (!dev_priv->objects)
  4501. goto err_out;
  4502. dev_priv->vmas = KMEM_CACHE(i915_vma, SLAB_HWCACHE_ALIGN);
  4503. if (!dev_priv->vmas)
  4504. goto err_objects;
  4505. dev_priv->luts = KMEM_CACHE(i915_lut_handle, 0);
  4506. if (!dev_priv->luts)
  4507. goto err_vmas;
  4508. dev_priv->requests = KMEM_CACHE(drm_i915_gem_request,
  4509. SLAB_HWCACHE_ALIGN |
  4510. SLAB_RECLAIM_ACCOUNT |
  4511. SLAB_TYPESAFE_BY_RCU);
  4512. if (!dev_priv->requests)
  4513. goto err_luts;
  4514. dev_priv->dependencies = KMEM_CACHE(i915_dependency,
  4515. SLAB_HWCACHE_ALIGN |
  4516. SLAB_RECLAIM_ACCOUNT);
  4517. if (!dev_priv->dependencies)
  4518. goto err_requests;
  4519. dev_priv->priorities = KMEM_CACHE(i915_priolist, SLAB_HWCACHE_ALIGN);
  4520. if (!dev_priv->priorities)
  4521. goto err_dependencies;
  4522. mutex_lock(&dev_priv->drm.struct_mutex);
  4523. INIT_LIST_HEAD(&dev_priv->gt.timelines);
  4524. err = i915_gem_timeline_init__global(dev_priv);
  4525. mutex_unlock(&dev_priv->drm.struct_mutex);
  4526. if (err)
  4527. goto err_priorities;
  4528. i915_gem_init__mm(dev_priv);
  4529. INIT_DELAYED_WORK(&dev_priv->gt.retire_work,
  4530. i915_gem_retire_work_handler);
  4531. INIT_DELAYED_WORK(&dev_priv->gt.idle_work,
  4532. i915_gem_idle_work_handler);
  4533. init_waitqueue_head(&dev_priv->gpu_error.wait_queue);
  4534. init_waitqueue_head(&dev_priv->gpu_error.reset_queue);
  4535. atomic_set(&dev_priv->mm.bsd_engine_dispatch_index, 0);
  4536. spin_lock_init(&dev_priv->fb_tracking.lock);
  4537. err = i915_gemfs_init(dev_priv);
  4538. if (err)
  4539. DRM_NOTE("Unable to create a private tmpfs mount, hugepage support will be disabled(%d).\n", err);
  4540. return 0;
  4541. err_priorities:
  4542. kmem_cache_destroy(dev_priv->priorities);
  4543. err_dependencies:
  4544. kmem_cache_destroy(dev_priv->dependencies);
  4545. err_requests:
  4546. kmem_cache_destroy(dev_priv->requests);
  4547. err_luts:
  4548. kmem_cache_destroy(dev_priv->luts);
  4549. err_vmas:
  4550. kmem_cache_destroy(dev_priv->vmas);
  4551. err_objects:
  4552. kmem_cache_destroy(dev_priv->objects);
  4553. err_out:
  4554. return err;
  4555. }
  4556. void i915_gem_load_cleanup(struct drm_i915_private *dev_priv)
  4557. {
  4558. i915_gem_drain_freed_objects(dev_priv);
  4559. WARN_ON(!llist_empty(&dev_priv->mm.free_list));
  4560. WARN_ON(dev_priv->mm.object_count);
  4561. mutex_lock(&dev_priv->drm.struct_mutex);
  4562. i915_gem_timeline_fini(&dev_priv->gt.global_timeline);
  4563. WARN_ON(!list_empty(&dev_priv->gt.timelines));
  4564. mutex_unlock(&dev_priv->drm.struct_mutex);
  4565. kmem_cache_destroy(dev_priv->priorities);
  4566. kmem_cache_destroy(dev_priv->dependencies);
  4567. kmem_cache_destroy(dev_priv->requests);
  4568. kmem_cache_destroy(dev_priv->luts);
  4569. kmem_cache_destroy(dev_priv->vmas);
  4570. kmem_cache_destroy(dev_priv->objects);
  4571. /* And ensure that our DESTROY_BY_RCU slabs are truly destroyed */
  4572. rcu_barrier();
  4573. i915_gemfs_fini(dev_priv);
  4574. }
  4575. int i915_gem_freeze(struct drm_i915_private *dev_priv)
  4576. {
  4577. /* Discard all purgeable objects, let userspace recover those as
  4578. * required after resuming.
  4579. */
  4580. i915_gem_shrink_all(dev_priv);
  4581. return 0;
  4582. }
  4583. int i915_gem_freeze_late(struct drm_i915_private *dev_priv)
  4584. {
  4585. struct drm_i915_gem_object *obj;
  4586. struct list_head *phases[] = {
  4587. &dev_priv->mm.unbound_list,
  4588. &dev_priv->mm.bound_list,
  4589. NULL
  4590. }, **p;
  4591. /* Called just before we write the hibernation image.
  4592. *
  4593. * We need to update the domain tracking to reflect that the CPU
  4594. * will be accessing all the pages to create and restore from the
  4595. * hibernation, and so upon restoration those pages will be in the
  4596. * CPU domain.
  4597. *
  4598. * To make sure the hibernation image contains the latest state,
  4599. * we update that state just before writing out the image.
  4600. *
  4601. * To try and reduce the hibernation image, we manually shrink
  4602. * the objects as well, see i915_gem_freeze()
  4603. */
  4604. i915_gem_shrink(dev_priv, -1UL, NULL, I915_SHRINK_UNBOUND);
  4605. i915_gem_drain_freed_objects(dev_priv);
  4606. spin_lock(&dev_priv->mm.obj_lock);
  4607. for (p = phases; *p; p++) {
  4608. list_for_each_entry(obj, *p, mm.link)
  4609. __start_cpu_write(obj);
  4610. }
  4611. spin_unlock(&dev_priv->mm.obj_lock);
  4612. return 0;
  4613. }
  4614. void i915_gem_release(struct drm_device *dev, struct drm_file *file)
  4615. {
  4616. struct drm_i915_file_private *file_priv = file->driver_priv;
  4617. struct drm_i915_gem_request *request;
  4618. /* Clean up our request list when the client is going away, so that
  4619. * later retire_requests won't dereference our soon-to-be-gone
  4620. * file_priv.
  4621. */
  4622. spin_lock(&file_priv->mm.lock);
  4623. list_for_each_entry(request, &file_priv->mm.request_list, client_link)
  4624. request->file_priv = NULL;
  4625. spin_unlock(&file_priv->mm.lock);
  4626. }
  4627. int i915_gem_open(struct drm_i915_private *i915, struct drm_file *file)
  4628. {
  4629. struct drm_i915_file_private *file_priv;
  4630. int ret;
  4631. DRM_DEBUG("\n");
  4632. file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
  4633. if (!file_priv)
  4634. return -ENOMEM;
  4635. file->driver_priv = file_priv;
  4636. file_priv->dev_priv = i915;
  4637. file_priv->file = file;
  4638. spin_lock_init(&file_priv->mm.lock);
  4639. INIT_LIST_HEAD(&file_priv->mm.request_list);
  4640. file_priv->bsd_engine = -1;
  4641. ret = i915_gem_context_open(i915, file);
  4642. if (ret)
  4643. kfree(file_priv);
  4644. return ret;
  4645. }
  4646. /**
  4647. * i915_gem_track_fb - update frontbuffer tracking
  4648. * @old: current GEM buffer for the frontbuffer slots
  4649. * @new: new GEM buffer for the frontbuffer slots
  4650. * @frontbuffer_bits: bitmask of frontbuffer slots
  4651. *
  4652. * This updates the frontbuffer tracking bits @frontbuffer_bits by clearing them
  4653. * from @old and setting them in @new. Both @old and @new can be NULL.
  4654. */
  4655. void i915_gem_track_fb(struct drm_i915_gem_object *old,
  4656. struct drm_i915_gem_object *new,
  4657. unsigned frontbuffer_bits)
  4658. {
  4659. /* Control of individual bits within the mask are guarded by
  4660. * the owning plane->mutex, i.e. we can never see concurrent
  4661. * manipulation of individual bits. But since the bitfield as a whole
  4662. * is updated using RMW, we need to use atomics in order to update
  4663. * the bits.
  4664. */
  4665. BUILD_BUG_ON(INTEL_FRONTBUFFER_BITS_PER_PIPE * I915_MAX_PIPES >
  4666. sizeof(atomic_t) * BITS_PER_BYTE);
  4667. if (old) {
  4668. WARN_ON(!(atomic_read(&old->frontbuffer_bits) & frontbuffer_bits));
  4669. atomic_andnot(frontbuffer_bits, &old->frontbuffer_bits);
  4670. }
  4671. if (new) {
  4672. WARN_ON(atomic_read(&new->frontbuffer_bits) & frontbuffer_bits);
  4673. atomic_or(frontbuffer_bits, &new->frontbuffer_bits);
  4674. }
  4675. }
  4676. /* Allocate a new GEM object and fill it with the supplied data */
  4677. struct drm_i915_gem_object *
  4678. i915_gem_object_create_from_data(struct drm_i915_private *dev_priv,
  4679. const void *data, size_t size)
  4680. {
  4681. struct drm_i915_gem_object *obj;
  4682. struct file *file;
  4683. size_t offset;
  4684. int err;
  4685. obj = i915_gem_object_create(dev_priv, round_up(size, PAGE_SIZE));
  4686. if (IS_ERR(obj))
  4687. return obj;
  4688. GEM_BUG_ON(obj->base.write_domain != I915_GEM_DOMAIN_CPU);
  4689. file = obj->base.filp;
  4690. offset = 0;
  4691. do {
  4692. unsigned int len = min_t(typeof(size), size, PAGE_SIZE);
  4693. struct page *page;
  4694. void *pgdata, *vaddr;
  4695. err = pagecache_write_begin(file, file->f_mapping,
  4696. offset, len, 0,
  4697. &page, &pgdata);
  4698. if (err < 0)
  4699. goto fail;
  4700. vaddr = kmap(page);
  4701. memcpy(vaddr, data, len);
  4702. kunmap(page);
  4703. err = pagecache_write_end(file, file->f_mapping,
  4704. offset, len, len,
  4705. page, pgdata);
  4706. if (err < 0)
  4707. goto fail;
  4708. size -= len;
  4709. data += len;
  4710. offset += len;
  4711. } while (size);
  4712. return obj;
  4713. fail:
  4714. i915_gem_object_put(obj);
  4715. return ERR_PTR(err);
  4716. }
  4717. struct scatterlist *
  4718. i915_gem_object_get_sg(struct drm_i915_gem_object *obj,
  4719. unsigned int n,
  4720. unsigned int *offset)
  4721. {
  4722. struct i915_gem_object_page_iter *iter = &obj->mm.get_page;
  4723. struct scatterlist *sg;
  4724. unsigned int idx, count;
  4725. might_sleep();
  4726. GEM_BUG_ON(n >= obj->base.size >> PAGE_SHIFT);
  4727. GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
  4728. /* As we iterate forward through the sg, we record each entry in a
  4729. * radixtree for quick repeated (backwards) lookups. If we have seen
  4730. * this index previously, we will have an entry for it.
  4731. *
  4732. * Initial lookup is O(N), but this is amortized to O(1) for
  4733. * sequential page access (where each new request is consecutive
  4734. * to the previous one). Repeated lookups are O(lg(obj->base.size)),
  4735. * i.e. O(1) with a large constant!
  4736. */
  4737. if (n < READ_ONCE(iter->sg_idx))
  4738. goto lookup;
  4739. mutex_lock(&iter->lock);
  4740. /* We prefer to reuse the last sg so that repeated lookup of this
  4741. * (or the subsequent) sg are fast - comparing against the last
  4742. * sg is faster than going through the radixtree.
  4743. */
  4744. sg = iter->sg_pos;
  4745. idx = iter->sg_idx;
  4746. count = __sg_page_count(sg);
  4747. while (idx + count <= n) {
  4748. unsigned long exception, i;
  4749. int ret;
  4750. /* If we cannot allocate and insert this entry, or the
  4751. * individual pages from this range, cancel updating the
  4752. * sg_idx so that on this lookup we are forced to linearly
  4753. * scan onwards, but on future lookups we will try the
  4754. * insertion again (in which case we need to be careful of
  4755. * the error return reporting that we have already inserted
  4756. * this index).
  4757. */
  4758. ret = radix_tree_insert(&iter->radix, idx, sg);
  4759. if (ret && ret != -EEXIST)
  4760. goto scan;
  4761. exception =
  4762. RADIX_TREE_EXCEPTIONAL_ENTRY |
  4763. idx << RADIX_TREE_EXCEPTIONAL_SHIFT;
  4764. for (i = 1; i < count; i++) {
  4765. ret = radix_tree_insert(&iter->radix, idx + i,
  4766. (void *)exception);
  4767. if (ret && ret != -EEXIST)
  4768. goto scan;
  4769. }
  4770. idx += count;
  4771. sg = ____sg_next(sg);
  4772. count = __sg_page_count(sg);
  4773. }
  4774. scan:
  4775. iter->sg_pos = sg;
  4776. iter->sg_idx = idx;
  4777. mutex_unlock(&iter->lock);
  4778. if (unlikely(n < idx)) /* insertion completed by another thread */
  4779. goto lookup;
  4780. /* In case we failed to insert the entry into the radixtree, we need
  4781. * to look beyond the current sg.
  4782. */
  4783. while (idx + count <= n) {
  4784. idx += count;
  4785. sg = ____sg_next(sg);
  4786. count = __sg_page_count(sg);
  4787. }
  4788. *offset = n - idx;
  4789. return sg;
  4790. lookup:
  4791. rcu_read_lock();
  4792. sg = radix_tree_lookup(&iter->radix, n);
  4793. GEM_BUG_ON(!sg);
  4794. /* If this index is in the middle of multi-page sg entry,
  4795. * the radixtree will contain an exceptional entry that points
  4796. * to the start of that range. We will return the pointer to
  4797. * the base page and the offset of this page within the
  4798. * sg entry's range.
  4799. */
  4800. *offset = 0;
  4801. if (unlikely(radix_tree_exception(sg))) {
  4802. unsigned long base =
  4803. (unsigned long)sg >> RADIX_TREE_EXCEPTIONAL_SHIFT;
  4804. sg = radix_tree_lookup(&iter->radix, base);
  4805. GEM_BUG_ON(!sg);
  4806. *offset = n - base;
  4807. }
  4808. rcu_read_unlock();
  4809. return sg;
  4810. }
  4811. struct page *
  4812. i915_gem_object_get_page(struct drm_i915_gem_object *obj, unsigned int n)
  4813. {
  4814. struct scatterlist *sg;
  4815. unsigned int offset;
  4816. GEM_BUG_ON(!i915_gem_object_has_struct_page(obj));
  4817. sg = i915_gem_object_get_sg(obj, n, &offset);
  4818. return nth_page(sg_page(sg), offset);
  4819. }
  4820. /* Like i915_gem_object_get_page(), but mark the returned page dirty */
  4821. struct page *
  4822. i915_gem_object_get_dirty_page(struct drm_i915_gem_object *obj,
  4823. unsigned int n)
  4824. {
  4825. struct page *page;
  4826. page = i915_gem_object_get_page(obj, n);
  4827. if (!obj->mm.dirty)
  4828. set_page_dirty(page);
  4829. return page;
  4830. }
  4831. dma_addr_t
  4832. i915_gem_object_get_dma_address(struct drm_i915_gem_object *obj,
  4833. unsigned long n)
  4834. {
  4835. struct scatterlist *sg;
  4836. unsigned int offset;
  4837. sg = i915_gem_object_get_sg(obj, n, &offset);
  4838. return sg_dma_address(sg) + (offset << PAGE_SHIFT);
  4839. }
  4840. int i915_gem_object_attach_phys(struct drm_i915_gem_object *obj, int align)
  4841. {
  4842. struct sg_table *pages;
  4843. int err;
  4844. if (align > obj->base.size)
  4845. return -EINVAL;
  4846. if (obj->ops == &i915_gem_phys_ops)
  4847. return 0;
  4848. if (obj->ops != &i915_gem_object_ops)
  4849. return -EINVAL;
  4850. err = i915_gem_object_unbind(obj);
  4851. if (err)
  4852. return err;
  4853. mutex_lock(&obj->mm.lock);
  4854. if (obj->mm.madv != I915_MADV_WILLNEED) {
  4855. err = -EFAULT;
  4856. goto err_unlock;
  4857. }
  4858. if (obj->mm.quirked) {
  4859. err = -EFAULT;
  4860. goto err_unlock;
  4861. }
  4862. if (obj->mm.mapping) {
  4863. err = -EBUSY;
  4864. goto err_unlock;
  4865. }
  4866. pages = fetch_and_zero(&obj->mm.pages);
  4867. if (pages) {
  4868. struct drm_i915_private *i915 = to_i915(obj->base.dev);
  4869. __i915_gem_object_reset_page_iter(obj);
  4870. spin_lock(&i915->mm.obj_lock);
  4871. list_del(&obj->mm.link);
  4872. spin_unlock(&i915->mm.obj_lock);
  4873. }
  4874. obj->ops = &i915_gem_phys_ops;
  4875. err = ____i915_gem_object_get_pages(obj);
  4876. if (err)
  4877. goto err_xfer;
  4878. /* Perma-pin (until release) the physical set of pages */
  4879. __i915_gem_object_pin_pages(obj);
  4880. if (!IS_ERR_OR_NULL(pages))
  4881. i915_gem_object_ops.put_pages(obj, pages);
  4882. mutex_unlock(&obj->mm.lock);
  4883. return 0;
  4884. err_xfer:
  4885. obj->ops = &i915_gem_object_ops;
  4886. obj->mm.pages = pages;
  4887. err_unlock:
  4888. mutex_unlock(&obj->mm.lock);
  4889. return err;
  4890. }
  4891. #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
  4892. #include "selftests/scatterlist.c"
  4893. #include "selftests/mock_gem_device.c"
  4894. #include "selftests/huge_gem_object.c"
  4895. #include "selftests/huge_pages.c"
  4896. #include "selftests/i915_gem_object.c"
  4897. #include "selftests/i915_gem_coherency.c"
  4898. #endif