i915_gem.c 136 KB

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