i915_gem.c 144 KB

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