i915_gem.c 137 KB

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