i915_gem.c 134 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133
  1. /*
  2. * Copyright © 2008 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. * IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Eric Anholt <eric@anholt.net>
  25. *
  26. */
  27. #include "drmP.h"
  28. #include "drm.h"
  29. #include "i915_drm.h"
  30. #include "i915_drv.h"
  31. #include "i915_trace.h"
  32. #include "intel_drv.h"
  33. #include <linux/slab.h>
  34. #include <linux/swap.h>
  35. #include <linux/pci.h>
  36. static void i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj);
  37. static void i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj);
  38. static void i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj);
  39. static int i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj,
  40. int write);
  41. static int i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
  42. uint64_t offset,
  43. uint64_t size);
  44. static void i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj);
  45. static int i915_gem_object_wait_rendering(struct drm_gem_object *obj);
  46. static int i915_gem_object_bind_to_gtt(struct drm_gem_object *obj,
  47. unsigned alignment);
  48. static void i915_gem_clear_fence_reg(struct drm_gem_object *obj);
  49. static int i915_gem_evict_something(struct drm_device *dev, int min_size);
  50. static int i915_gem_evict_from_inactive_list(struct drm_device *dev);
  51. static int i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
  52. struct drm_i915_gem_pwrite *args,
  53. struct drm_file *file_priv);
  54. static LIST_HEAD(shrink_list);
  55. static DEFINE_SPINLOCK(shrink_list_lock);
  56. int i915_gem_do_init(struct drm_device *dev, unsigned long start,
  57. unsigned long end)
  58. {
  59. drm_i915_private_t *dev_priv = dev->dev_private;
  60. if (start >= end ||
  61. (start & (PAGE_SIZE - 1)) != 0 ||
  62. (end & (PAGE_SIZE - 1)) != 0) {
  63. return -EINVAL;
  64. }
  65. drm_mm_init(&dev_priv->mm.gtt_space, start,
  66. end - start);
  67. dev->gtt_total = (uint32_t) (end - start);
  68. return 0;
  69. }
  70. int
  71. i915_gem_init_ioctl(struct drm_device *dev, void *data,
  72. struct drm_file *file_priv)
  73. {
  74. struct drm_i915_gem_init *args = data;
  75. int ret;
  76. mutex_lock(&dev->struct_mutex);
  77. ret = i915_gem_do_init(dev, args->gtt_start, args->gtt_end);
  78. mutex_unlock(&dev->struct_mutex);
  79. return ret;
  80. }
  81. int
  82. i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
  83. struct drm_file *file_priv)
  84. {
  85. struct drm_i915_gem_get_aperture *args = data;
  86. if (!(dev->driver->driver_features & DRIVER_GEM))
  87. return -ENODEV;
  88. args->aper_size = dev->gtt_total;
  89. args->aper_available_size = (args->aper_size -
  90. atomic_read(&dev->pin_memory));
  91. return 0;
  92. }
  93. /**
  94. * Creates a new mm object and returns a handle to it.
  95. */
  96. int
  97. i915_gem_create_ioctl(struct drm_device *dev, void *data,
  98. struct drm_file *file_priv)
  99. {
  100. struct drm_i915_gem_create *args = data;
  101. struct drm_gem_object *obj;
  102. int ret;
  103. u32 handle;
  104. args->size = roundup(args->size, PAGE_SIZE);
  105. /* Allocate the new object */
  106. obj = i915_gem_alloc_object(dev, args->size);
  107. if (obj == NULL)
  108. return -ENOMEM;
  109. ret = drm_gem_handle_create(file_priv, obj, &handle);
  110. drm_gem_object_handle_unreference_unlocked(obj);
  111. if (ret)
  112. return ret;
  113. args->handle = handle;
  114. return 0;
  115. }
  116. static inline int
  117. fast_shmem_read(struct page **pages,
  118. loff_t page_base, int page_offset,
  119. char __user *data,
  120. int length)
  121. {
  122. char __iomem *vaddr;
  123. int unwritten;
  124. vaddr = kmap_atomic(pages[page_base >> PAGE_SHIFT], KM_USER0);
  125. if (vaddr == NULL)
  126. return -ENOMEM;
  127. unwritten = __copy_to_user_inatomic(data, vaddr + page_offset, length);
  128. kunmap_atomic(vaddr, KM_USER0);
  129. if (unwritten)
  130. return -EFAULT;
  131. return 0;
  132. }
  133. static int i915_gem_object_needs_bit17_swizzle(struct drm_gem_object *obj)
  134. {
  135. drm_i915_private_t *dev_priv = obj->dev->dev_private;
  136. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  137. return dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_9_10_17 &&
  138. obj_priv->tiling_mode != I915_TILING_NONE;
  139. }
  140. static inline void
  141. slow_shmem_copy(struct page *dst_page,
  142. int dst_offset,
  143. struct page *src_page,
  144. int src_offset,
  145. int length)
  146. {
  147. char *dst_vaddr, *src_vaddr;
  148. dst_vaddr = kmap(dst_page);
  149. src_vaddr = kmap(src_page);
  150. memcpy(dst_vaddr + dst_offset, src_vaddr + src_offset, length);
  151. kunmap(src_page);
  152. kunmap(dst_page);
  153. }
  154. static inline void
  155. slow_shmem_bit17_copy(struct page *gpu_page,
  156. int gpu_offset,
  157. struct page *cpu_page,
  158. int cpu_offset,
  159. int length,
  160. int is_read)
  161. {
  162. char *gpu_vaddr, *cpu_vaddr;
  163. /* Use the unswizzled path if this page isn't affected. */
  164. if ((page_to_phys(gpu_page) & (1 << 17)) == 0) {
  165. if (is_read)
  166. return slow_shmem_copy(cpu_page, cpu_offset,
  167. gpu_page, gpu_offset, length);
  168. else
  169. return slow_shmem_copy(gpu_page, gpu_offset,
  170. cpu_page, cpu_offset, length);
  171. }
  172. gpu_vaddr = kmap(gpu_page);
  173. cpu_vaddr = kmap(cpu_page);
  174. /* Copy the data, XORing A6 with A17 (1). The user already knows he's
  175. * XORing with the other bits (A9 for Y, A9 and A10 for X)
  176. */
  177. while (length > 0) {
  178. int cacheline_end = ALIGN(gpu_offset + 1, 64);
  179. int this_length = min(cacheline_end - gpu_offset, length);
  180. int swizzled_gpu_offset = gpu_offset ^ 64;
  181. if (is_read) {
  182. memcpy(cpu_vaddr + cpu_offset,
  183. gpu_vaddr + swizzled_gpu_offset,
  184. this_length);
  185. } else {
  186. memcpy(gpu_vaddr + swizzled_gpu_offset,
  187. cpu_vaddr + cpu_offset,
  188. this_length);
  189. }
  190. cpu_offset += this_length;
  191. gpu_offset += this_length;
  192. length -= this_length;
  193. }
  194. kunmap(cpu_page);
  195. kunmap(gpu_page);
  196. }
  197. /**
  198. * This is the fast shmem pread path, which attempts to copy_from_user directly
  199. * from the backing pages of the object to the user's address space. On a
  200. * fault, it fails so we can fall back to i915_gem_shmem_pwrite_slow().
  201. */
  202. static int
  203. i915_gem_shmem_pread_fast(struct drm_device *dev, struct drm_gem_object *obj,
  204. struct drm_i915_gem_pread *args,
  205. struct drm_file *file_priv)
  206. {
  207. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  208. ssize_t remain;
  209. loff_t offset, page_base;
  210. char __user *user_data;
  211. int page_offset, page_length;
  212. int ret;
  213. user_data = (char __user *) (uintptr_t) args->data_ptr;
  214. remain = args->size;
  215. mutex_lock(&dev->struct_mutex);
  216. ret = i915_gem_object_get_pages(obj, 0);
  217. if (ret != 0)
  218. goto fail_unlock;
  219. ret = i915_gem_object_set_cpu_read_domain_range(obj, args->offset,
  220. args->size);
  221. if (ret != 0)
  222. goto fail_put_pages;
  223. obj_priv = to_intel_bo(obj);
  224. offset = args->offset;
  225. while (remain > 0) {
  226. /* Operation in this page
  227. *
  228. * page_base = page offset within aperture
  229. * page_offset = offset within page
  230. * page_length = bytes to copy for this page
  231. */
  232. page_base = (offset & ~(PAGE_SIZE-1));
  233. page_offset = offset & (PAGE_SIZE-1);
  234. page_length = remain;
  235. if ((page_offset + remain) > PAGE_SIZE)
  236. page_length = PAGE_SIZE - page_offset;
  237. ret = fast_shmem_read(obj_priv->pages,
  238. page_base, page_offset,
  239. user_data, page_length);
  240. if (ret)
  241. goto fail_put_pages;
  242. remain -= page_length;
  243. user_data += page_length;
  244. offset += page_length;
  245. }
  246. fail_put_pages:
  247. i915_gem_object_put_pages(obj);
  248. fail_unlock:
  249. mutex_unlock(&dev->struct_mutex);
  250. return ret;
  251. }
  252. static int
  253. i915_gem_object_get_pages_or_evict(struct drm_gem_object *obj)
  254. {
  255. int ret;
  256. ret = i915_gem_object_get_pages(obj, __GFP_NORETRY | __GFP_NOWARN);
  257. /* If we've insufficient memory to map in the pages, attempt
  258. * to make some space by throwing out some old buffers.
  259. */
  260. if (ret == -ENOMEM) {
  261. struct drm_device *dev = obj->dev;
  262. ret = i915_gem_evict_something(dev, obj->size);
  263. if (ret)
  264. return ret;
  265. ret = i915_gem_object_get_pages(obj, 0);
  266. }
  267. return ret;
  268. }
  269. /**
  270. * This is the fallback shmem pread path, which allocates temporary storage
  271. * in kernel space to copy_to_user into outside of the struct_mutex, so we
  272. * can copy out of the object's backing pages while holding the struct mutex
  273. * and not take page faults.
  274. */
  275. static int
  276. i915_gem_shmem_pread_slow(struct drm_device *dev, struct drm_gem_object *obj,
  277. struct drm_i915_gem_pread *args,
  278. struct drm_file *file_priv)
  279. {
  280. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  281. struct mm_struct *mm = current->mm;
  282. struct page **user_pages;
  283. ssize_t remain;
  284. loff_t offset, pinned_pages, i;
  285. loff_t first_data_page, last_data_page, num_pages;
  286. int shmem_page_index, shmem_page_offset;
  287. int data_page_index, data_page_offset;
  288. int page_length;
  289. int ret;
  290. uint64_t data_ptr = args->data_ptr;
  291. int do_bit17_swizzling;
  292. remain = args->size;
  293. /* Pin the user pages containing the data. We can't fault while
  294. * holding the struct mutex, yet we want to hold it while
  295. * dereferencing the user data.
  296. */
  297. first_data_page = data_ptr / PAGE_SIZE;
  298. last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
  299. num_pages = last_data_page - first_data_page + 1;
  300. user_pages = drm_calloc_large(num_pages, sizeof(struct page *));
  301. if (user_pages == NULL)
  302. return -ENOMEM;
  303. down_read(&mm->mmap_sem);
  304. pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
  305. num_pages, 1, 0, user_pages, NULL);
  306. up_read(&mm->mmap_sem);
  307. if (pinned_pages < num_pages) {
  308. ret = -EFAULT;
  309. goto fail_put_user_pages;
  310. }
  311. do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
  312. mutex_lock(&dev->struct_mutex);
  313. ret = i915_gem_object_get_pages_or_evict(obj);
  314. if (ret)
  315. goto fail_unlock;
  316. ret = i915_gem_object_set_cpu_read_domain_range(obj, args->offset,
  317. args->size);
  318. if (ret != 0)
  319. goto fail_put_pages;
  320. obj_priv = to_intel_bo(obj);
  321. offset = args->offset;
  322. while (remain > 0) {
  323. /* Operation in this page
  324. *
  325. * shmem_page_index = page number within shmem file
  326. * shmem_page_offset = offset within page in shmem file
  327. * data_page_index = page number in get_user_pages return
  328. * data_page_offset = offset with data_page_index page.
  329. * page_length = bytes to copy for this page
  330. */
  331. shmem_page_index = offset / PAGE_SIZE;
  332. shmem_page_offset = offset & ~PAGE_MASK;
  333. data_page_index = data_ptr / PAGE_SIZE - first_data_page;
  334. data_page_offset = data_ptr & ~PAGE_MASK;
  335. page_length = remain;
  336. if ((shmem_page_offset + page_length) > PAGE_SIZE)
  337. page_length = PAGE_SIZE - shmem_page_offset;
  338. if ((data_page_offset + page_length) > PAGE_SIZE)
  339. page_length = PAGE_SIZE - data_page_offset;
  340. if (do_bit17_swizzling) {
  341. slow_shmem_bit17_copy(obj_priv->pages[shmem_page_index],
  342. shmem_page_offset,
  343. user_pages[data_page_index],
  344. data_page_offset,
  345. page_length,
  346. 1);
  347. } else {
  348. slow_shmem_copy(user_pages[data_page_index],
  349. data_page_offset,
  350. obj_priv->pages[shmem_page_index],
  351. shmem_page_offset,
  352. page_length);
  353. }
  354. remain -= page_length;
  355. data_ptr += page_length;
  356. offset += page_length;
  357. }
  358. fail_put_pages:
  359. i915_gem_object_put_pages(obj);
  360. fail_unlock:
  361. mutex_unlock(&dev->struct_mutex);
  362. fail_put_user_pages:
  363. for (i = 0; i < pinned_pages; i++) {
  364. SetPageDirty(user_pages[i]);
  365. page_cache_release(user_pages[i]);
  366. }
  367. drm_free_large(user_pages);
  368. return ret;
  369. }
  370. /**
  371. * Reads data from the object referenced by handle.
  372. *
  373. * On error, the contents of *data are undefined.
  374. */
  375. int
  376. i915_gem_pread_ioctl(struct drm_device *dev, void *data,
  377. struct drm_file *file_priv)
  378. {
  379. struct drm_i915_gem_pread *args = data;
  380. struct drm_gem_object *obj;
  381. struct drm_i915_gem_object *obj_priv;
  382. int ret;
  383. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  384. if (obj == NULL)
  385. return -EBADF;
  386. obj_priv = to_intel_bo(obj);
  387. /* Bounds check source.
  388. *
  389. * XXX: This could use review for overflow issues...
  390. */
  391. if (args->offset > obj->size || args->size > obj->size ||
  392. args->offset + args->size > obj->size) {
  393. drm_gem_object_unreference_unlocked(obj);
  394. return -EINVAL;
  395. }
  396. if (i915_gem_object_needs_bit17_swizzle(obj)) {
  397. ret = i915_gem_shmem_pread_slow(dev, obj, args, file_priv);
  398. } else {
  399. ret = i915_gem_shmem_pread_fast(dev, obj, args, file_priv);
  400. if (ret != 0)
  401. ret = i915_gem_shmem_pread_slow(dev, obj, args,
  402. file_priv);
  403. }
  404. drm_gem_object_unreference_unlocked(obj);
  405. return ret;
  406. }
  407. /* This is the fast write path which cannot handle
  408. * page faults in the source data
  409. */
  410. static inline int
  411. fast_user_write(struct io_mapping *mapping,
  412. loff_t page_base, int page_offset,
  413. char __user *user_data,
  414. int length)
  415. {
  416. char *vaddr_atomic;
  417. unsigned long unwritten;
  418. vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
  419. unwritten = __copy_from_user_inatomic_nocache(vaddr_atomic + page_offset,
  420. user_data, length);
  421. io_mapping_unmap_atomic(vaddr_atomic);
  422. if (unwritten)
  423. return -EFAULT;
  424. return 0;
  425. }
  426. /* Here's the write path which can sleep for
  427. * page faults
  428. */
  429. static inline void
  430. slow_kernel_write(struct io_mapping *mapping,
  431. loff_t gtt_base, int gtt_offset,
  432. struct page *user_page, int user_offset,
  433. int length)
  434. {
  435. char __iomem *dst_vaddr;
  436. char *src_vaddr;
  437. dst_vaddr = io_mapping_map_wc(mapping, gtt_base);
  438. src_vaddr = kmap(user_page);
  439. memcpy_toio(dst_vaddr + gtt_offset,
  440. src_vaddr + user_offset,
  441. length);
  442. kunmap(user_page);
  443. io_mapping_unmap(dst_vaddr);
  444. }
  445. static inline int
  446. fast_shmem_write(struct page **pages,
  447. loff_t page_base, int page_offset,
  448. char __user *data,
  449. int length)
  450. {
  451. char __iomem *vaddr;
  452. unsigned long unwritten;
  453. vaddr = kmap_atomic(pages[page_base >> PAGE_SHIFT], KM_USER0);
  454. if (vaddr == NULL)
  455. return -ENOMEM;
  456. unwritten = __copy_from_user_inatomic(vaddr + page_offset, data, length);
  457. kunmap_atomic(vaddr, KM_USER0);
  458. if (unwritten)
  459. return -EFAULT;
  460. return 0;
  461. }
  462. /**
  463. * This is the fast pwrite path, where we copy the data directly from the
  464. * user into the GTT, uncached.
  465. */
  466. static int
  467. i915_gem_gtt_pwrite_fast(struct drm_device *dev, struct drm_gem_object *obj,
  468. struct drm_i915_gem_pwrite *args,
  469. struct drm_file *file_priv)
  470. {
  471. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  472. drm_i915_private_t *dev_priv = dev->dev_private;
  473. ssize_t remain;
  474. loff_t offset, page_base;
  475. char __user *user_data;
  476. int page_offset, page_length;
  477. int ret;
  478. user_data = (char __user *) (uintptr_t) args->data_ptr;
  479. remain = args->size;
  480. if (!access_ok(VERIFY_READ, user_data, remain))
  481. return -EFAULT;
  482. mutex_lock(&dev->struct_mutex);
  483. ret = i915_gem_object_pin(obj, 0);
  484. if (ret) {
  485. mutex_unlock(&dev->struct_mutex);
  486. return ret;
  487. }
  488. ret = i915_gem_object_set_to_gtt_domain(obj, 1);
  489. if (ret)
  490. goto fail;
  491. obj_priv = to_intel_bo(obj);
  492. offset = obj_priv->gtt_offset + args->offset;
  493. while (remain > 0) {
  494. /* Operation in this page
  495. *
  496. * page_base = page offset within aperture
  497. * page_offset = offset within page
  498. * page_length = bytes to copy for this page
  499. */
  500. page_base = (offset & ~(PAGE_SIZE-1));
  501. page_offset = offset & (PAGE_SIZE-1);
  502. page_length = remain;
  503. if ((page_offset + remain) > PAGE_SIZE)
  504. page_length = PAGE_SIZE - page_offset;
  505. ret = fast_user_write (dev_priv->mm.gtt_mapping, page_base,
  506. page_offset, user_data, page_length);
  507. /* If we get a fault while copying data, then (presumably) our
  508. * source page isn't available. Return the error and we'll
  509. * retry in the slow path.
  510. */
  511. if (ret)
  512. goto fail;
  513. remain -= page_length;
  514. user_data += page_length;
  515. offset += page_length;
  516. }
  517. fail:
  518. i915_gem_object_unpin(obj);
  519. mutex_unlock(&dev->struct_mutex);
  520. return ret;
  521. }
  522. /**
  523. * This is the fallback GTT pwrite path, which uses get_user_pages to pin
  524. * the memory and maps it using kmap_atomic for copying.
  525. *
  526. * This code resulted in x11perf -rgb10text consuming about 10% more CPU
  527. * than using i915_gem_gtt_pwrite_fast on a G45 (32-bit).
  528. */
  529. static int
  530. i915_gem_gtt_pwrite_slow(struct drm_device *dev, struct drm_gem_object *obj,
  531. struct drm_i915_gem_pwrite *args,
  532. struct drm_file *file_priv)
  533. {
  534. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  535. drm_i915_private_t *dev_priv = dev->dev_private;
  536. ssize_t remain;
  537. loff_t gtt_page_base, offset;
  538. loff_t first_data_page, last_data_page, num_pages;
  539. loff_t pinned_pages, i;
  540. struct page **user_pages;
  541. struct mm_struct *mm = current->mm;
  542. int gtt_page_offset, data_page_offset, data_page_index, page_length;
  543. int ret;
  544. uint64_t data_ptr = args->data_ptr;
  545. remain = args->size;
  546. /* Pin the user pages containing the data. We can't fault while
  547. * holding the struct mutex, and all of the pwrite implementations
  548. * want to hold it while dereferencing the user data.
  549. */
  550. first_data_page = data_ptr / PAGE_SIZE;
  551. last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
  552. num_pages = last_data_page - first_data_page + 1;
  553. user_pages = drm_calloc_large(num_pages, sizeof(struct page *));
  554. if (user_pages == NULL)
  555. return -ENOMEM;
  556. down_read(&mm->mmap_sem);
  557. pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
  558. num_pages, 0, 0, user_pages, NULL);
  559. up_read(&mm->mmap_sem);
  560. if (pinned_pages < num_pages) {
  561. ret = -EFAULT;
  562. goto out_unpin_pages;
  563. }
  564. mutex_lock(&dev->struct_mutex);
  565. ret = i915_gem_object_pin(obj, 0);
  566. if (ret)
  567. goto out_unlock;
  568. ret = i915_gem_object_set_to_gtt_domain(obj, 1);
  569. if (ret)
  570. goto out_unpin_object;
  571. obj_priv = to_intel_bo(obj);
  572. offset = obj_priv->gtt_offset + args->offset;
  573. while (remain > 0) {
  574. /* Operation in this page
  575. *
  576. * gtt_page_base = page offset within aperture
  577. * gtt_page_offset = offset within page in aperture
  578. * data_page_index = page number in get_user_pages return
  579. * data_page_offset = offset with data_page_index page.
  580. * page_length = bytes to copy for this page
  581. */
  582. gtt_page_base = offset & PAGE_MASK;
  583. gtt_page_offset = offset & ~PAGE_MASK;
  584. data_page_index = data_ptr / PAGE_SIZE - first_data_page;
  585. data_page_offset = data_ptr & ~PAGE_MASK;
  586. page_length = remain;
  587. if ((gtt_page_offset + page_length) > PAGE_SIZE)
  588. page_length = PAGE_SIZE - gtt_page_offset;
  589. if ((data_page_offset + page_length) > PAGE_SIZE)
  590. page_length = PAGE_SIZE - data_page_offset;
  591. slow_kernel_write(dev_priv->mm.gtt_mapping,
  592. gtt_page_base, gtt_page_offset,
  593. user_pages[data_page_index],
  594. data_page_offset,
  595. page_length);
  596. remain -= page_length;
  597. offset += page_length;
  598. data_ptr += page_length;
  599. }
  600. out_unpin_object:
  601. i915_gem_object_unpin(obj);
  602. out_unlock:
  603. mutex_unlock(&dev->struct_mutex);
  604. out_unpin_pages:
  605. for (i = 0; i < pinned_pages; i++)
  606. page_cache_release(user_pages[i]);
  607. drm_free_large(user_pages);
  608. return ret;
  609. }
  610. /**
  611. * This is the fast shmem pwrite path, which attempts to directly
  612. * copy_from_user into the kmapped pages backing the object.
  613. */
  614. static int
  615. i915_gem_shmem_pwrite_fast(struct drm_device *dev, struct drm_gem_object *obj,
  616. struct drm_i915_gem_pwrite *args,
  617. struct drm_file *file_priv)
  618. {
  619. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  620. ssize_t remain;
  621. loff_t offset, page_base;
  622. char __user *user_data;
  623. int page_offset, page_length;
  624. int ret;
  625. user_data = (char __user *) (uintptr_t) args->data_ptr;
  626. remain = args->size;
  627. mutex_lock(&dev->struct_mutex);
  628. ret = i915_gem_object_get_pages(obj, 0);
  629. if (ret != 0)
  630. goto fail_unlock;
  631. ret = i915_gem_object_set_to_cpu_domain(obj, 1);
  632. if (ret != 0)
  633. goto fail_put_pages;
  634. obj_priv = to_intel_bo(obj);
  635. offset = args->offset;
  636. obj_priv->dirty = 1;
  637. while (remain > 0) {
  638. /* Operation in this page
  639. *
  640. * page_base = page offset within aperture
  641. * page_offset = offset within page
  642. * page_length = bytes to copy for this page
  643. */
  644. page_base = (offset & ~(PAGE_SIZE-1));
  645. page_offset = offset & (PAGE_SIZE-1);
  646. page_length = remain;
  647. if ((page_offset + remain) > PAGE_SIZE)
  648. page_length = PAGE_SIZE - page_offset;
  649. ret = fast_shmem_write(obj_priv->pages,
  650. page_base, page_offset,
  651. user_data, page_length);
  652. if (ret)
  653. goto fail_put_pages;
  654. remain -= page_length;
  655. user_data += page_length;
  656. offset += page_length;
  657. }
  658. fail_put_pages:
  659. i915_gem_object_put_pages(obj);
  660. fail_unlock:
  661. mutex_unlock(&dev->struct_mutex);
  662. return ret;
  663. }
  664. /**
  665. * This is the fallback shmem pwrite path, which uses get_user_pages to pin
  666. * the memory and maps it using kmap_atomic for copying.
  667. *
  668. * This avoids taking mmap_sem for faulting on the user's address while the
  669. * struct_mutex is held.
  670. */
  671. static int
  672. i915_gem_shmem_pwrite_slow(struct drm_device *dev, struct drm_gem_object *obj,
  673. struct drm_i915_gem_pwrite *args,
  674. struct drm_file *file_priv)
  675. {
  676. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  677. struct mm_struct *mm = current->mm;
  678. struct page **user_pages;
  679. ssize_t remain;
  680. loff_t offset, pinned_pages, i;
  681. loff_t first_data_page, last_data_page, num_pages;
  682. int shmem_page_index, shmem_page_offset;
  683. int data_page_index, data_page_offset;
  684. int page_length;
  685. int ret;
  686. uint64_t data_ptr = args->data_ptr;
  687. int do_bit17_swizzling;
  688. remain = args->size;
  689. /* Pin the user pages containing the data. We can't fault while
  690. * holding the struct mutex, and all of the pwrite implementations
  691. * want to hold it while dereferencing the user data.
  692. */
  693. first_data_page = data_ptr / PAGE_SIZE;
  694. last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
  695. num_pages = last_data_page - first_data_page + 1;
  696. user_pages = drm_calloc_large(num_pages, sizeof(struct page *));
  697. if (user_pages == NULL)
  698. return -ENOMEM;
  699. down_read(&mm->mmap_sem);
  700. pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
  701. num_pages, 0, 0, user_pages, NULL);
  702. up_read(&mm->mmap_sem);
  703. if (pinned_pages < num_pages) {
  704. ret = -EFAULT;
  705. goto fail_put_user_pages;
  706. }
  707. do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
  708. mutex_lock(&dev->struct_mutex);
  709. ret = i915_gem_object_get_pages_or_evict(obj);
  710. if (ret)
  711. goto fail_unlock;
  712. ret = i915_gem_object_set_to_cpu_domain(obj, 1);
  713. if (ret != 0)
  714. goto fail_put_pages;
  715. obj_priv = to_intel_bo(obj);
  716. offset = args->offset;
  717. obj_priv->dirty = 1;
  718. while (remain > 0) {
  719. /* Operation in this page
  720. *
  721. * shmem_page_index = page number within shmem file
  722. * shmem_page_offset = offset within page in shmem file
  723. * data_page_index = page number in get_user_pages return
  724. * data_page_offset = offset with data_page_index page.
  725. * page_length = bytes to copy for this page
  726. */
  727. shmem_page_index = offset / PAGE_SIZE;
  728. shmem_page_offset = offset & ~PAGE_MASK;
  729. data_page_index = data_ptr / PAGE_SIZE - first_data_page;
  730. data_page_offset = data_ptr & ~PAGE_MASK;
  731. page_length = remain;
  732. if ((shmem_page_offset + page_length) > PAGE_SIZE)
  733. page_length = PAGE_SIZE - shmem_page_offset;
  734. if ((data_page_offset + page_length) > PAGE_SIZE)
  735. page_length = PAGE_SIZE - data_page_offset;
  736. if (do_bit17_swizzling) {
  737. slow_shmem_bit17_copy(obj_priv->pages[shmem_page_index],
  738. shmem_page_offset,
  739. user_pages[data_page_index],
  740. data_page_offset,
  741. page_length,
  742. 0);
  743. } else {
  744. slow_shmem_copy(obj_priv->pages[shmem_page_index],
  745. shmem_page_offset,
  746. user_pages[data_page_index],
  747. data_page_offset,
  748. page_length);
  749. }
  750. remain -= page_length;
  751. data_ptr += page_length;
  752. offset += page_length;
  753. }
  754. fail_put_pages:
  755. i915_gem_object_put_pages(obj);
  756. fail_unlock:
  757. mutex_unlock(&dev->struct_mutex);
  758. fail_put_user_pages:
  759. for (i = 0; i < pinned_pages; i++)
  760. page_cache_release(user_pages[i]);
  761. drm_free_large(user_pages);
  762. return ret;
  763. }
  764. /**
  765. * Writes data to the object referenced by handle.
  766. *
  767. * On error, the contents of the buffer that were to be modified are undefined.
  768. */
  769. int
  770. i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
  771. struct drm_file *file_priv)
  772. {
  773. struct drm_i915_gem_pwrite *args = data;
  774. struct drm_gem_object *obj;
  775. struct drm_i915_gem_object *obj_priv;
  776. int ret = 0;
  777. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  778. if (obj == NULL)
  779. return -EBADF;
  780. obj_priv = to_intel_bo(obj);
  781. /* Bounds check destination.
  782. *
  783. * XXX: This could use review for overflow issues...
  784. */
  785. if (args->offset > obj->size || args->size > obj->size ||
  786. args->offset + args->size > obj->size) {
  787. drm_gem_object_unreference_unlocked(obj);
  788. return -EINVAL;
  789. }
  790. /* We can only do the GTT pwrite on untiled buffers, as otherwise
  791. * it would end up going through the fenced access, and we'll get
  792. * different detiling behavior between reading and writing.
  793. * pread/pwrite currently are reading and writing from the CPU
  794. * perspective, requiring manual detiling by the client.
  795. */
  796. if (obj_priv->phys_obj)
  797. ret = i915_gem_phys_pwrite(dev, obj, args, file_priv);
  798. else if (obj_priv->tiling_mode == I915_TILING_NONE &&
  799. dev->gtt_total != 0 &&
  800. obj->write_domain != I915_GEM_DOMAIN_CPU) {
  801. ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file_priv);
  802. if (ret == -EFAULT) {
  803. ret = i915_gem_gtt_pwrite_slow(dev, obj, args,
  804. file_priv);
  805. }
  806. } else if (i915_gem_object_needs_bit17_swizzle(obj)) {
  807. ret = i915_gem_shmem_pwrite_slow(dev, obj, args, file_priv);
  808. } else {
  809. ret = i915_gem_shmem_pwrite_fast(dev, obj, args, file_priv);
  810. if (ret == -EFAULT) {
  811. ret = i915_gem_shmem_pwrite_slow(dev, obj, args,
  812. file_priv);
  813. }
  814. }
  815. #if WATCH_PWRITE
  816. if (ret)
  817. DRM_INFO("pwrite failed %d\n", ret);
  818. #endif
  819. drm_gem_object_unreference_unlocked(obj);
  820. return ret;
  821. }
  822. /**
  823. * Called when user space prepares to use an object with the CPU, either
  824. * through the mmap ioctl's mapping or a GTT mapping.
  825. */
  826. int
  827. i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
  828. struct drm_file *file_priv)
  829. {
  830. struct drm_i915_private *dev_priv = dev->dev_private;
  831. struct drm_i915_gem_set_domain *args = data;
  832. struct drm_gem_object *obj;
  833. struct drm_i915_gem_object *obj_priv;
  834. uint32_t read_domains = args->read_domains;
  835. uint32_t write_domain = args->write_domain;
  836. int ret;
  837. if (!(dev->driver->driver_features & DRIVER_GEM))
  838. return -ENODEV;
  839. /* Only handle setting domains to types used by the CPU. */
  840. if (write_domain & I915_GEM_GPU_DOMAINS)
  841. return -EINVAL;
  842. if (read_domains & I915_GEM_GPU_DOMAINS)
  843. return -EINVAL;
  844. /* Having something in the write domain implies it's in the read
  845. * domain, and only that read domain. Enforce that in the request.
  846. */
  847. if (write_domain != 0 && read_domains != write_domain)
  848. return -EINVAL;
  849. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  850. if (obj == NULL)
  851. return -EBADF;
  852. obj_priv = to_intel_bo(obj);
  853. mutex_lock(&dev->struct_mutex);
  854. intel_mark_busy(dev, obj);
  855. #if WATCH_BUF
  856. DRM_INFO("set_domain_ioctl %p(%zd), %08x %08x\n",
  857. obj, obj->size, read_domains, write_domain);
  858. #endif
  859. if (read_domains & I915_GEM_DOMAIN_GTT) {
  860. ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
  861. /* Update the LRU on the fence for the CPU access that's
  862. * about to occur.
  863. */
  864. if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
  865. struct drm_i915_fence_reg *reg =
  866. &dev_priv->fence_regs[obj_priv->fence_reg];
  867. list_move_tail(&reg->lru_list,
  868. &dev_priv->mm.fence_list);
  869. }
  870. /* Silently promote "you're not bound, there was nothing to do"
  871. * to success, since the client was just asking us to
  872. * make sure everything was done.
  873. */
  874. if (ret == -EINVAL)
  875. ret = 0;
  876. } else {
  877. ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
  878. }
  879. drm_gem_object_unreference(obj);
  880. mutex_unlock(&dev->struct_mutex);
  881. return ret;
  882. }
  883. /**
  884. * Called when user space has done writes to this buffer
  885. */
  886. int
  887. i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
  888. struct drm_file *file_priv)
  889. {
  890. struct drm_i915_gem_sw_finish *args = data;
  891. struct drm_gem_object *obj;
  892. struct drm_i915_gem_object *obj_priv;
  893. int ret = 0;
  894. if (!(dev->driver->driver_features & DRIVER_GEM))
  895. return -ENODEV;
  896. mutex_lock(&dev->struct_mutex);
  897. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  898. if (obj == NULL) {
  899. mutex_unlock(&dev->struct_mutex);
  900. return -EBADF;
  901. }
  902. #if WATCH_BUF
  903. DRM_INFO("%s: sw_finish %d (%p %zd)\n",
  904. __func__, args->handle, obj, obj->size);
  905. #endif
  906. obj_priv = to_intel_bo(obj);
  907. /* Pinned buffers may be scanout, so flush the cache */
  908. if (obj_priv->pin_count)
  909. i915_gem_object_flush_cpu_write_domain(obj);
  910. drm_gem_object_unreference(obj);
  911. mutex_unlock(&dev->struct_mutex);
  912. return ret;
  913. }
  914. /**
  915. * Maps the contents of an object, returning the address it is mapped
  916. * into.
  917. *
  918. * While the mapping holds a reference on the contents of the object, it doesn't
  919. * imply a ref on the object itself.
  920. */
  921. int
  922. i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
  923. struct drm_file *file_priv)
  924. {
  925. struct drm_i915_gem_mmap *args = data;
  926. struct drm_gem_object *obj;
  927. loff_t offset;
  928. unsigned long addr;
  929. if (!(dev->driver->driver_features & DRIVER_GEM))
  930. return -ENODEV;
  931. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  932. if (obj == NULL)
  933. return -EBADF;
  934. offset = args->offset;
  935. down_write(&current->mm->mmap_sem);
  936. addr = do_mmap(obj->filp, 0, args->size,
  937. PROT_READ | PROT_WRITE, MAP_SHARED,
  938. args->offset);
  939. up_write(&current->mm->mmap_sem);
  940. drm_gem_object_unreference_unlocked(obj);
  941. if (IS_ERR((void *)addr))
  942. return addr;
  943. args->addr_ptr = (uint64_t) addr;
  944. return 0;
  945. }
  946. /**
  947. * i915_gem_fault - fault a page into the GTT
  948. * vma: VMA in question
  949. * vmf: fault info
  950. *
  951. * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
  952. * from userspace. The fault handler takes care of binding the object to
  953. * the GTT (if needed), allocating and programming a fence register (again,
  954. * only if needed based on whether the old reg is still valid or the object
  955. * is tiled) and inserting a new PTE into the faulting process.
  956. *
  957. * Note that the faulting process may involve evicting existing objects
  958. * from the GTT and/or fence registers to make room. So performance may
  959. * suffer if the GTT working set is large or there are few fence registers
  960. * left.
  961. */
  962. int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  963. {
  964. struct drm_gem_object *obj = vma->vm_private_data;
  965. struct drm_device *dev = obj->dev;
  966. struct drm_i915_private *dev_priv = dev->dev_private;
  967. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  968. pgoff_t page_offset;
  969. unsigned long pfn;
  970. int ret = 0;
  971. bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
  972. /* We don't use vmf->pgoff since that has the fake offset */
  973. page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
  974. PAGE_SHIFT;
  975. /* Now bind it into the GTT if needed */
  976. mutex_lock(&dev->struct_mutex);
  977. if (!obj_priv->gtt_space) {
  978. ret = i915_gem_object_bind_to_gtt(obj, 0);
  979. if (ret)
  980. goto unlock;
  981. list_add_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
  982. ret = i915_gem_object_set_to_gtt_domain(obj, write);
  983. if (ret)
  984. goto unlock;
  985. }
  986. /* Need a new fence register? */
  987. if (obj_priv->tiling_mode != I915_TILING_NONE) {
  988. ret = i915_gem_object_get_fence_reg(obj);
  989. if (ret)
  990. goto unlock;
  991. }
  992. pfn = ((dev->agp->base + obj_priv->gtt_offset) >> PAGE_SHIFT) +
  993. page_offset;
  994. /* Finally, remap it using the new GTT offset */
  995. ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
  996. unlock:
  997. mutex_unlock(&dev->struct_mutex);
  998. switch (ret) {
  999. case 0:
  1000. case -ERESTARTSYS:
  1001. return VM_FAULT_NOPAGE;
  1002. case -ENOMEM:
  1003. case -EAGAIN:
  1004. return VM_FAULT_OOM;
  1005. default:
  1006. return VM_FAULT_SIGBUS;
  1007. }
  1008. }
  1009. /**
  1010. * i915_gem_create_mmap_offset - create a fake mmap offset for an object
  1011. * @obj: obj in question
  1012. *
  1013. * GEM memory mapping works by handing back to userspace a fake mmap offset
  1014. * it can use in a subsequent mmap(2) call. The DRM core code then looks
  1015. * up the object based on the offset and sets up the various memory mapping
  1016. * structures.
  1017. *
  1018. * This routine allocates and attaches a fake offset for @obj.
  1019. */
  1020. static int
  1021. i915_gem_create_mmap_offset(struct drm_gem_object *obj)
  1022. {
  1023. struct drm_device *dev = obj->dev;
  1024. struct drm_gem_mm *mm = dev->mm_private;
  1025. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  1026. struct drm_map_list *list;
  1027. struct drm_local_map *map;
  1028. int ret = 0;
  1029. /* Set the object up for mmap'ing */
  1030. list = &obj->map_list;
  1031. list->map = kzalloc(sizeof(struct drm_map_list), GFP_KERNEL);
  1032. if (!list->map)
  1033. return -ENOMEM;
  1034. map = list->map;
  1035. map->type = _DRM_GEM;
  1036. map->size = obj->size;
  1037. map->handle = obj;
  1038. /* Get a DRM GEM mmap offset allocated... */
  1039. list->file_offset_node = drm_mm_search_free(&mm->offset_manager,
  1040. obj->size / PAGE_SIZE, 0, 0);
  1041. if (!list->file_offset_node) {
  1042. DRM_ERROR("failed to allocate offset for bo %d\n", obj->name);
  1043. ret = -ENOMEM;
  1044. goto out_free_list;
  1045. }
  1046. list->file_offset_node = drm_mm_get_block(list->file_offset_node,
  1047. obj->size / PAGE_SIZE, 0);
  1048. if (!list->file_offset_node) {
  1049. ret = -ENOMEM;
  1050. goto out_free_list;
  1051. }
  1052. list->hash.key = list->file_offset_node->start;
  1053. if (drm_ht_insert_item(&mm->offset_hash, &list->hash)) {
  1054. DRM_ERROR("failed to add to map hash\n");
  1055. ret = -ENOMEM;
  1056. goto out_free_mm;
  1057. }
  1058. /* By now we should be all set, any drm_mmap request on the offset
  1059. * below will get to our mmap & fault handler */
  1060. obj_priv->mmap_offset = ((uint64_t) list->hash.key) << PAGE_SHIFT;
  1061. return 0;
  1062. out_free_mm:
  1063. drm_mm_put_block(list->file_offset_node);
  1064. out_free_list:
  1065. kfree(list->map);
  1066. return ret;
  1067. }
  1068. /**
  1069. * i915_gem_release_mmap - remove physical page mappings
  1070. * @obj: obj in question
  1071. *
  1072. * Preserve the reservation of the mmapping with the DRM core code, but
  1073. * relinquish ownership of the pages back to the system.
  1074. *
  1075. * It is vital that we remove the page mapping if we have mapped a tiled
  1076. * object through the GTT and then lose the fence register due to
  1077. * resource pressure. Similarly if the object has been moved out of the
  1078. * aperture, than pages mapped into userspace must be revoked. Removing the
  1079. * mapping will then trigger a page fault on the next user access, allowing
  1080. * fixup by i915_gem_fault().
  1081. */
  1082. void
  1083. i915_gem_release_mmap(struct drm_gem_object *obj)
  1084. {
  1085. struct drm_device *dev = obj->dev;
  1086. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  1087. if (dev->dev_mapping)
  1088. unmap_mapping_range(dev->dev_mapping,
  1089. obj_priv->mmap_offset, obj->size, 1);
  1090. }
  1091. static void
  1092. i915_gem_free_mmap_offset(struct drm_gem_object *obj)
  1093. {
  1094. struct drm_device *dev = obj->dev;
  1095. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  1096. struct drm_gem_mm *mm = dev->mm_private;
  1097. struct drm_map_list *list;
  1098. list = &obj->map_list;
  1099. drm_ht_remove_item(&mm->offset_hash, &list->hash);
  1100. if (list->file_offset_node) {
  1101. drm_mm_put_block(list->file_offset_node);
  1102. list->file_offset_node = NULL;
  1103. }
  1104. if (list->map) {
  1105. kfree(list->map);
  1106. list->map = NULL;
  1107. }
  1108. obj_priv->mmap_offset = 0;
  1109. }
  1110. /**
  1111. * i915_gem_get_gtt_alignment - return required GTT alignment for an object
  1112. * @obj: object to check
  1113. *
  1114. * Return the required GTT alignment for an object, taking into account
  1115. * potential fence register mapping if needed.
  1116. */
  1117. static uint32_t
  1118. i915_gem_get_gtt_alignment(struct drm_gem_object *obj)
  1119. {
  1120. struct drm_device *dev = obj->dev;
  1121. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  1122. int start, i;
  1123. /*
  1124. * Minimum alignment is 4k (GTT page size), but might be greater
  1125. * if a fence register is needed for the object.
  1126. */
  1127. if (IS_I965G(dev) || obj_priv->tiling_mode == I915_TILING_NONE)
  1128. return 4096;
  1129. /*
  1130. * Previous chips need to be aligned to the size of the smallest
  1131. * fence register that can contain the object.
  1132. */
  1133. if (IS_I9XX(dev))
  1134. start = 1024*1024;
  1135. else
  1136. start = 512*1024;
  1137. for (i = start; i < obj->size; i <<= 1)
  1138. ;
  1139. return i;
  1140. }
  1141. /**
  1142. * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
  1143. * @dev: DRM device
  1144. * @data: GTT mapping ioctl data
  1145. * @file_priv: GEM object info
  1146. *
  1147. * Simply returns the fake offset to userspace so it can mmap it.
  1148. * The mmap call will end up in drm_gem_mmap(), which will set things
  1149. * up so we can get faults in the handler above.
  1150. *
  1151. * The fault handler will take care of binding the object into the GTT
  1152. * (since it may have been evicted to make room for something), allocating
  1153. * a fence register, and mapping the appropriate aperture address into
  1154. * userspace.
  1155. */
  1156. int
  1157. i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
  1158. struct drm_file *file_priv)
  1159. {
  1160. struct drm_i915_gem_mmap_gtt *args = data;
  1161. struct drm_i915_private *dev_priv = dev->dev_private;
  1162. struct drm_gem_object *obj;
  1163. struct drm_i915_gem_object *obj_priv;
  1164. int ret;
  1165. if (!(dev->driver->driver_features & DRIVER_GEM))
  1166. return -ENODEV;
  1167. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  1168. if (obj == NULL)
  1169. return -EBADF;
  1170. mutex_lock(&dev->struct_mutex);
  1171. obj_priv = to_intel_bo(obj);
  1172. if (obj_priv->madv != I915_MADV_WILLNEED) {
  1173. DRM_ERROR("Attempting to mmap a purgeable buffer\n");
  1174. drm_gem_object_unreference(obj);
  1175. mutex_unlock(&dev->struct_mutex);
  1176. return -EINVAL;
  1177. }
  1178. if (!obj_priv->mmap_offset) {
  1179. ret = i915_gem_create_mmap_offset(obj);
  1180. if (ret) {
  1181. drm_gem_object_unreference(obj);
  1182. mutex_unlock(&dev->struct_mutex);
  1183. return ret;
  1184. }
  1185. }
  1186. args->offset = obj_priv->mmap_offset;
  1187. /*
  1188. * Pull it into the GTT so that we have a page list (makes the
  1189. * initial fault faster and any subsequent flushing possible).
  1190. */
  1191. if (!obj_priv->agp_mem) {
  1192. ret = i915_gem_object_bind_to_gtt(obj, 0);
  1193. if (ret) {
  1194. drm_gem_object_unreference(obj);
  1195. mutex_unlock(&dev->struct_mutex);
  1196. return ret;
  1197. }
  1198. list_add_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
  1199. }
  1200. drm_gem_object_unreference(obj);
  1201. mutex_unlock(&dev->struct_mutex);
  1202. return 0;
  1203. }
  1204. void
  1205. i915_gem_object_put_pages(struct drm_gem_object *obj)
  1206. {
  1207. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  1208. int page_count = obj->size / PAGE_SIZE;
  1209. int i;
  1210. BUG_ON(obj_priv->pages_refcount == 0);
  1211. BUG_ON(obj_priv->madv == __I915_MADV_PURGED);
  1212. if (--obj_priv->pages_refcount != 0)
  1213. return;
  1214. if (obj_priv->tiling_mode != I915_TILING_NONE)
  1215. i915_gem_object_save_bit_17_swizzle(obj);
  1216. if (obj_priv->madv == I915_MADV_DONTNEED)
  1217. obj_priv->dirty = 0;
  1218. for (i = 0; i < page_count; i++) {
  1219. if (obj_priv->dirty)
  1220. set_page_dirty(obj_priv->pages[i]);
  1221. if (obj_priv->madv == I915_MADV_WILLNEED)
  1222. mark_page_accessed(obj_priv->pages[i]);
  1223. page_cache_release(obj_priv->pages[i]);
  1224. }
  1225. obj_priv->dirty = 0;
  1226. drm_free_large(obj_priv->pages);
  1227. obj_priv->pages = NULL;
  1228. }
  1229. static void
  1230. i915_gem_object_move_to_active(struct drm_gem_object *obj, uint32_t seqno,
  1231. struct intel_ring_buffer *ring)
  1232. {
  1233. struct drm_device *dev = obj->dev;
  1234. drm_i915_private_t *dev_priv = dev->dev_private;
  1235. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  1236. BUG_ON(ring == NULL);
  1237. obj_priv->ring = ring;
  1238. /* Add a reference if we're newly entering the active list. */
  1239. if (!obj_priv->active) {
  1240. drm_gem_object_reference(obj);
  1241. obj_priv->active = 1;
  1242. }
  1243. /* Move from whatever list we were on to the tail of execution. */
  1244. spin_lock(&dev_priv->mm.active_list_lock);
  1245. list_move_tail(&obj_priv->list, &ring->active_list);
  1246. spin_unlock(&dev_priv->mm.active_list_lock);
  1247. obj_priv->last_rendering_seqno = seqno;
  1248. }
  1249. static void
  1250. i915_gem_object_move_to_flushing(struct drm_gem_object *obj)
  1251. {
  1252. struct drm_device *dev = obj->dev;
  1253. drm_i915_private_t *dev_priv = dev->dev_private;
  1254. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  1255. BUG_ON(!obj_priv->active);
  1256. list_move_tail(&obj_priv->list, &dev_priv->mm.flushing_list);
  1257. obj_priv->last_rendering_seqno = 0;
  1258. }
  1259. /* Immediately discard the backing storage */
  1260. static void
  1261. i915_gem_object_truncate(struct drm_gem_object *obj)
  1262. {
  1263. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  1264. struct inode *inode;
  1265. inode = obj->filp->f_path.dentry->d_inode;
  1266. if (inode->i_op->truncate)
  1267. inode->i_op->truncate (inode);
  1268. obj_priv->madv = __I915_MADV_PURGED;
  1269. }
  1270. static inline int
  1271. i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj_priv)
  1272. {
  1273. return obj_priv->madv == I915_MADV_DONTNEED;
  1274. }
  1275. static void
  1276. i915_gem_object_move_to_inactive(struct drm_gem_object *obj)
  1277. {
  1278. struct drm_device *dev = obj->dev;
  1279. drm_i915_private_t *dev_priv = dev->dev_private;
  1280. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  1281. i915_verify_inactive(dev, __FILE__, __LINE__);
  1282. if (obj_priv->pin_count != 0)
  1283. list_del_init(&obj_priv->list);
  1284. else
  1285. list_move_tail(&obj_priv->list, &dev_priv->mm.inactive_list);
  1286. BUG_ON(!list_empty(&obj_priv->gpu_write_list));
  1287. obj_priv->last_rendering_seqno = 0;
  1288. obj_priv->ring = NULL;
  1289. if (obj_priv->active) {
  1290. obj_priv->active = 0;
  1291. drm_gem_object_unreference(obj);
  1292. }
  1293. i915_verify_inactive(dev, __FILE__, __LINE__);
  1294. }
  1295. static void
  1296. i915_gem_process_flushing_list(struct drm_device *dev,
  1297. uint32_t flush_domains, uint32_t seqno,
  1298. struct intel_ring_buffer *ring)
  1299. {
  1300. drm_i915_private_t *dev_priv = dev->dev_private;
  1301. struct drm_i915_gem_object *obj_priv, *next;
  1302. list_for_each_entry_safe(obj_priv, next,
  1303. &dev_priv->mm.gpu_write_list,
  1304. gpu_write_list) {
  1305. struct drm_gem_object *obj = &obj_priv->base;
  1306. if ((obj->write_domain & flush_domains) ==
  1307. obj->write_domain &&
  1308. obj_priv->ring->ring_flag == ring->ring_flag) {
  1309. uint32_t old_write_domain = obj->write_domain;
  1310. obj->write_domain = 0;
  1311. list_del_init(&obj_priv->gpu_write_list);
  1312. i915_gem_object_move_to_active(obj, seqno, ring);
  1313. /* update the fence lru list */
  1314. if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
  1315. struct drm_i915_fence_reg *reg =
  1316. &dev_priv->fence_regs[obj_priv->fence_reg];
  1317. list_move_tail(&reg->lru_list,
  1318. &dev_priv->mm.fence_list);
  1319. }
  1320. trace_i915_gem_object_change_domain(obj,
  1321. obj->read_domains,
  1322. old_write_domain);
  1323. }
  1324. }
  1325. }
  1326. uint32_t
  1327. i915_add_request(struct drm_device *dev, struct drm_file *file_priv,
  1328. uint32_t flush_domains, struct intel_ring_buffer *ring)
  1329. {
  1330. drm_i915_private_t *dev_priv = dev->dev_private;
  1331. struct drm_i915_file_private *i915_file_priv = NULL;
  1332. struct drm_i915_gem_request *request;
  1333. uint32_t seqno;
  1334. int was_empty;
  1335. if (file_priv != NULL)
  1336. i915_file_priv = file_priv->driver_priv;
  1337. request = kzalloc(sizeof(*request), GFP_KERNEL);
  1338. if (request == NULL)
  1339. return 0;
  1340. seqno = ring->add_request(dev, ring, file_priv, flush_domains);
  1341. request->seqno = seqno;
  1342. request->ring = ring;
  1343. request->emitted_jiffies = jiffies;
  1344. was_empty = list_empty(&ring->request_list);
  1345. list_add_tail(&request->list, &ring->request_list);
  1346. if (i915_file_priv) {
  1347. list_add_tail(&request->client_list,
  1348. &i915_file_priv->mm.request_list);
  1349. } else {
  1350. INIT_LIST_HEAD(&request->client_list);
  1351. }
  1352. /* Associate any objects on the flushing list matching the write
  1353. * domain we're flushing with our flush.
  1354. */
  1355. if (flush_domains != 0)
  1356. i915_gem_process_flushing_list(dev, flush_domains, seqno, ring);
  1357. if (!dev_priv->mm.suspended) {
  1358. mod_timer(&dev_priv->hangcheck_timer, jiffies + DRM_I915_HANGCHECK_PERIOD);
  1359. if (was_empty)
  1360. queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
  1361. }
  1362. return seqno;
  1363. }
  1364. /**
  1365. * Command execution barrier
  1366. *
  1367. * Ensures that all commands in the ring are finished
  1368. * before signalling the CPU
  1369. */
  1370. static uint32_t
  1371. i915_retire_commands(struct drm_device *dev, struct intel_ring_buffer *ring)
  1372. {
  1373. uint32_t flush_domains = 0;
  1374. /* The sampler always gets flushed on i965 (sigh) */
  1375. if (IS_I965G(dev))
  1376. flush_domains |= I915_GEM_DOMAIN_SAMPLER;
  1377. ring->flush(dev, ring,
  1378. I915_GEM_DOMAIN_COMMAND, flush_domains);
  1379. return flush_domains;
  1380. }
  1381. /**
  1382. * Moves buffers associated only with the given active seqno from the active
  1383. * to inactive list, potentially freeing them.
  1384. */
  1385. static void
  1386. i915_gem_retire_request(struct drm_device *dev,
  1387. struct drm_i915_gem_request *request)
  1388. {
  1389. drm_i915_private_t *dev_priv = dev->dev_private;
  1390. trace_i915_gem_request_retire(dev, request->seqno);
  1391. /* Move any buffers on the active list that are no longer referenced
  1392. * by the ringbuffer to the flushing/inactive lists as appropriate.
  1393. */
  1394. spin_lock(&dev_priv->mm.active_list_lock);
  1395. while (!list_empty(&request->ring->active_list)) {
  1396. struct drm_gem_object *obj;
  1397. struct drm_i915_gem_object *obj_priv;
  1398. obj_priv = list_first_entry(&request->ring->active_list,
  1399. struct drm_i915_gem_object,
  1400. list);
  1401. obj = &obj_priv->base;
  1402. /* If the seqno being retired doesn't match the oldest in the
  1403. * list, then the oldest in the list must still be newer than
  1404. * this seqno.
  1405. */
  1406. if (obj_priv->last_rendering_seqno != request->seqno)
  1407. goto out;
  1408. #if WATCH_LRU
  1409. DRM_INFO("%s: retire %d moves to inactive list %p\n",
  1410. __func__, request->seqno, obj);
  1411. #endif
  1412. if (obj->write_domain != 0)
  1413. i915_gem_object_move_to_flushing(obj);
  1414. else {
  1415. /* Take a reference on the object so it won't be
  1416. * freed while the spinlock is held. The list
  1417. * protection for this spinlock is safe when breaking
  1418. * the lock like this since the next thing we do
  1419. * is just get the head of the list again.
  1420. */
  1421. drm_gem_object_reference(obj);
  1422. i915_gem_object_move_to_inactive(obj);
  1423. spin_unlock(&dev_priv->mm.active_list_lock);
  1424. drm_gem_object_unreference(obj);
  1425. spin_lock(&dev_priv->mm.active_list_lock);
  1426. }
  1427. }
  1428. out:
  1429. spin_unlock(&dev_priv->mm.active_list_lock);
  1430. }
  1431. /**
  1432. * Returns true if seq1 is later than seq2.
  1433. */
  1434. bool
  1435. i915_seqno_passed(uint32_t seq1, uint32_t seq2)
  1436. {
  1437. return (int32_t)(seq1 - seq2) >= 0;
  1438. }
  1439. uint32_t
  1440. i915_get_gem_seqno(struct drm_device *dev,
  1441. struct intel_ring_buffer *ring)
  1442. {
  1443. return ring->get_gem_seqno(dev, ring);
  1444. }
  1445. /**
  1446. * This function clears the request list as sequence numbers are passed.
  1447. */
  1448. void
  1449. i915_gem_retire_requests(struct drm_device *dev,
  1450. struct intel_ring_buffer *ring)
  1451. {
  1452. drm_i915_private_t *dev_priv = dev->dev_private;
  1453. uint32_t seqno;
  1454. if (!ring->status_page.page_addr
  1455. || list_empty(&ring->request_list))
  1456. return;
  1457. seqno = i915_get_gem_seqno(dev, ring);
  1458. while (!list_empty(&ring->request_list)) {
  1459. struct drm_i915_gem_request *request;
  1460. uint32_t retiring_seqno;
  1461. request = list_first_entry(&ring->request_list,
  1462. struct drm_i915_gem_request,
  1463. list);
  1464. retiring_seqno = request->seqno;
  1465. if (i915_seqno_passed(seqno, retiring_seqno) ||
  1466. atomic_read(&dev_priv->mm.wedged)) {
  1467. i915_gem_retire_request(dev, request);
  1468. list_del(&request->list);
  1469. list_del(&request->client_list);
  1470. kfree(request);
  1471. } else
  1472. break;
  1473. }
  1474. if (unlikely (dev_priv->trace_irq_seqno &&
  1475. i915_seqno_passed(dev_priv->trace_irq_seqno, seqno))) {
  1476. ring->user_irq_put(dev, ring);
  1477. dev_priv->trace_irq_seqno = 0;
  1478. }
  1479. }
  1480. void
  1481. i915_gem_retire_work_handler(struct work_struct *work)
  1482. {
  1483. drm_i915_private_t *dev_priv;
  1484. struct drm_device *dev;
  1485. dev_priv = container_of(work, drm_i915_private_t,
  1486. mm.retire_work.work);
  1487. dev = dev_priv->dev;
  1488. mutex_lock(&dev->struct_mutex);
  1489. i915_gem_retire_requests(dev, &dev_priv->render_ring);
  1490. if (HAS_BSD(dev))
  1491. i915_gem_retire_requests(dev, &dev_priv->bsd_ring);
  1492. if (!dev_priv->mm.suspended &&
  1493. (!list_empty(&dev_priv->render_ring.request_list) ||
  1494. (HAS_BSD(dev) &&
  1495. !list_empty(&dev_priv->bsd_ring.request_list))))
  1496. queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
  1497. mutex_unlock(&dev->struct_mutex);
  1498. }
  1499. int
  1500. i915_do_wait_request(struct drm_device *dev, uint32_t seqno,
  1501. int interruptible, struct intel_ring_buffer *ring)
  1502. {
  1503. drm_i915_private_t *dev_priv = dev->dev_private;
  1504. u32 ier;
  1505. int ret = 0;
  1506. BUG_ON(seqno == 0);
  1507. if (atomic_read(&dev_priv->mm.wedged))
  1508. return -EIO;
  1509. if (!i915_seqno_passed(ring->get_gem_seqno(dev, ring), seqno)) {
  1510. if (HAS_PCH_SPLIT(dev))
  1511. ier = I915_READ(DEIER) | I915_READ(GTIER);
  1512. else
  1513. ier = I915_READ(IER);
  1514. if (!ier) {
  1515. DRM_ERROR("something (likely vbetool) disabled "
  1516. "interrupts, re-enabling\n");
  1517. i915_driver_irq_preinstall(dev);
  1518. i915_driver_irq_postinstall(dev);
  1519. }
  1520. trace_i915_gem_request_wait_begin(dev, seqno);
  1521. ring->waiting_gem_seqno = seqno;
  1522. ring->user_irq_get(dev, ring);
  1523. if (interruptible)
  1524. ret = wait_event_interruptible(ring->irq_queue,
  1525. i915_seqno_passed(
  1526. ring->get_gem_seqno(dev, ring), seqno)
  1527. || atomic_read(&dev_priv->mm.wedged));
  1528. else
  1529. wait_event(ring->irq_queue,
  1530. i915_seqno_passed(
  1531. ring->get_gem_seqno(dev, ring), seqno)
  1532. || atomic_read(&dev_priv->mm.wedged));
  1533. ring->user_irq_put(dev, ring);
  1534. ring->waiting_gem_seqno = 0;
  1535. trace_i915_gem_request_wait_end(dev, seqno);
  1536. }
  1537. if (atomic_read(&dev_priv->mm.wedged))
  1538. ret = -EIO;
  1539. if (ret && ret != -ERESTARTSYS)
  1540. DRM_ERROR("%s returns %d (awaiting %d at %d)\n",
  1541. __func__, ret, seqno, ring->get_gem_seqno(dev, ring));
  1542. /* Directly dispatch request retiring. While we have the work queue
  1543. * to handle this, the waiter on a request often wants an associated
  1544. * buffer to have made it to the inactive list, and we would need
  1545. * a separate wait queue to handle that.
  1546. */
  1547. if (ret == 0)
  1548. i915_gem_retire_requests(dev, ring);
  1549. return ret;
  1550. }
  1551. /**
  1552. * Waits for a sequence number to be signaled, and cleans up the
  1553. * request and object lists appropriately for that event.
  1554. */
  1555. static int
  1556. i915_wait_request(struct drm_device *dev, uint32_t seqno,
  1557. struct intel_ring_buffer *ring)
  1558. {
  1559. return i915_do_wait_request(dev, seqno, 1, ring);
  1560. }
  1561. static void
  1562. i915_gem_flush(struct drm_device *dev,
  1563. uint32_t invalidate_domains,
  1564. uint32_t flush_domains)
  1565. {
  1566. drm_i915_private_t *dev_priv = dev->dev_private;
  1567. if (flush_domains & I915_GEM_DOMAIN_CPU)
  1568. drm_agp_chipset_flush(dev);
  1569. dev_priv->render_ring.flush(dev, &dev_priv->render_ring,
  1570. invalidate_domains,
  1571. flush_domains);
  1572. if (HAS_BSD(dev))
  1573. dev_priv->bsd_ring.flush(dev, &dev_priv->bsd_ring,
  1574. invalidate_domains,
  1575. flush_domains);
  1576. }
  1577. static void
  1578. i915_gem_flush_ring(struct drm_device *dev,
  1579. uint32_t invalidate_domains,
  1580. uint32_t flush_domains,
  1581. struct intel_ring_buffer *ring)
  1582. {
  1583. if (flush_domains & I915_GEM_DOMAIN_CPU)
  1584. drm_agp_chipset_flush(dev);
  1585. ring->flush(dev, ring,
  1586. invalidate_domains,
  1587. flush_domains);
  1588. }
  1589. /**
  1590. * Ensures that all rendering to the object has completed and the object is
  1591. * safe to unbind from the GTT or access from the CPU.
  1592. */
  1593. static int
  1594. i915_gem_object_wait_rendering(struct drm_gem_object *obj)
  1595. {
  1596. struct drm_device *dev = obj->dev;
  1597. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  1598. int ret;
  1599. /* This function only exists to support waiting for existing rendering,
  1600. * not for emitting required flushes.
  1601. */
  1602. BUG_ON((obj->write_domain & I915_GEM_GPU_DOMAINS) != 0);
  1603. /* If there is rendering queued on the buffer being evicted, wait for
  1604. * it.
  1605. */
  1606. if (obj_priv->active) {
  1607. #if WATCH_BUF
  1608. DRM_INFO("%s: object %p wait for seqno %08x\n",
  1609. __func__, obj, obj_priv->last_rendering_seqno);
  1610. #endif
  1611. ret = i915_wait_request(dev,
  1612. obj_priv->last_rendering_seqno, obj_priv->ring);
  1613. if (ret != 0)
  1614. return ret;
  1615. }
  1616. return 0;
  1617. }
  1618. /**
  1619. * Unbinds an object from the GTT aperture.
  1620. */
  1621. int
  1622. i915_gem_object_unbind(struct drm_gem_object *obj)
  1623. {
  1624. struct drm_device *dev = obj->dev;
  1625. drm_i915_private_t *dev_priv = dev->dev_private;
  1626. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  1627. int ret = 0;
  1628. #if WATCH_BUF
  1629. DRM_INFO("%s:%d %p\n", __func__, __LINE__, obj);
  1630. DRM_INFO("gtt_space %p\n", obj_priv->gtt_space);
  1631. #endif
  1632. if (obj_priv->gtt_space == NULL)
  1633. return 0;
  1634. if (obj_priv->pin_count != 0) {
  1635. DRM_ERROR("Attempting to unbind pinned buffer\n");
  1636. return -EINVAL;
  1637. }
  1638. /* blow away mappings if mapped through GTT */
  1639. i915_gem_release_mmap(obj);
  1640. /* Move the object to the CPU domain to ensure that
  1641. * any possible CPU writes while it's not in the GTT
  1642. * are flushed when we go to remap it. This will
  1643. * also ensure that all pending GPU writes are finished
  1644. * before we unbind.
  1645. */
  1646. ret = i915_gem_object_set_to_cpu_domain(obj, 1);
  1647. if (ret) {
  1648. if (ret != -ERESTARTSYS)
  1649. DRM_ERROR("set_domain failed: %d\n", ret);
  1650. return ret;
  1651. }
  1652. BUG_ON(obj_priv->active);
  1653. /* release the fence reg _after_ flushing */
  1654. if (obj_priv->fence_reg != I915_FENCE_REG_NONE)
  1655. i915_gem_clear_fence_reg(obj);
  1656. if (obj_priv->agp_mem != NULL) {
  1657. drm_unbind_agp(obj_priv->agp_mem);
  1658. drm_free_agp(obj_priv->agp_mem, obj->size / PAGE_SIZE);
  1659. obj_priv->agp_mem = NULL;
  1660. }
  1661. i915_gem_object_put_pages(obj);
  1662. BUG_ON(obj_priv->pages_refcount);
  1663. if (obj_priv->gtt_space) {
  1664. atomic_dec(&dev->gtt_count);
  1665. atomic_sub(obj->size, &dev->gtt_memory);
  1666. drm_mm_put_block(obj_priv->gtt_space);
  1667. obj_priv->gtt_space = NULL;
  1668. }
  1669. /* Remove ourselves from the LRU list if present. */
  1670. spin_lock(&dev_priv->mm.active_list_lock);
  1671. if (!list_empty(&obj_priv->list))
  1672. list_del_init(&obj_priv->list);
  1673. spin_unlock(&dev_priv->mm.active_list_lock);
  1674. if (i915_gem_object_is_purgeable(obj_priv))
  1675. i915_gem_object_truncate(obj);
  1676. trace_i915_gem_object_unbind(obj);
  1677. return 0;
  1678. }
  1679. static struct drm_gem_object *
  1680. i915_gem_find_inactive_object(struct drm_device *dev, int min_size)
  1681. {
  1682. drm_i915_private_t *dev_priv = dev->dev_private;
  1683. struct drm_i915_gem_object *obj_priv;
  1684. struct drm_gem_object *best = NULL;
  1685. struct drm_gem_object *first = NULL;
  1686. /* Try to find the smallest clean object */
  1687. list_for_each_entry(obj_priv, &dev_priv->mm.inactive_list, list) {
  1688. struct drm_gem_object *obj = &obj_priv->base;
  1689. if (obj->size >= min_size) {
  1690. if ((!obj_priv->dirty ||
  1691. i915_gem_object_is_purgeable(obj_priv)) &&
  1692. (!best || obj->size < best->size)) {
  1693. best = obj;
  1694. if (best->size == min_size)
  1695. return best;
  1696. }
  1697. if (!first)
  1698. first = obj;
  1699. }
  1700. }
  1701. return best ? best : first;
  1702. }
  1703. static int
  1704. i915_gpu_idle(struct drm_device *dev)
  1705. {
  1706. drm_i915_private_t *dev_priv = dev->dev_private;
  1707. bool lists_empty;
  1708. uint32_t seqno1, seqno2;
  1709. int ret;
  1710. spin_lock(&dev_priv->mm.active_list_lock);
  1711. lists_empty = (list_empty(&dev_priv->mm.flushing_list) &&
  1712. list_empty(&dev_priv->render_ring.active_list) &&
  1713. (!HAS_BSD(dev) ||
  1714. list_empty(&dev_priv->bsd_ring.active_list)));
  1715. spin_unlock(&dev_priv->mm.active_list_lock);
  1716. if (lists_empty)
  1717. return 0;
  1718. /* Flush everything onto the inactive list. */
  1719. i915_gem_flush(dev, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
  1720. seqno1 = i915_add_request(dev, NULL, I915_GEM_GPU_DOMAINS,
  1721. &dev_priv->render_ring);
  1722. if (seqno1 == 0)
  1723. return -ENOMEM;
  1724. ret = i915_wait_request(dev, seqno1, &dev_priv->render_ring);
  1725. if (HAS_BSD(dev)) {
  1726. seqno2 = i915_add_request(dev, NULL, I915_GEM_GPU_DOMAINS,
  1727. &dev_priv->bsd_ring);
  1728. if (seqno2 == 0)
  1729. return -ENOMEM;
  1730. ret = i915_wait_request(dev, seqno2, &dev_priv->bsd_ring);
  1731. if (ret)
  1732. return ret;
  1733. }
  1734. return ret;
  1735. }
  1736. static int
  1737. i915_gem_evict_everything(struct drm_device *dev)
  1738. {
  1739. drm_i915_private_t *dev_priv = dev->dev_private;
  1740. int ret;
  1741. bool lists_empty;
  1742. spin_lock(&dev_priv->mm.active_list_lock);
  1743. lists_empty = (list_empty(&dev_priv->mm.inactive_list) &&
  1744. list_empty(&dev_priv->mm.flushing_list) &&
  1745. list_empty(&dev_priv->render_ring.active_list) &&
  1746. (!HAS_BSD(dev)
  1747. || list_empty(&dev_priv->bsd_ring.active_list)));
  1748. spin_unlock(&dev_priv->mm.active_list_lock);
  1749. if (lists_empty)
  1750. return -ENOSPC;
  1751. /* Flush everything (on to the inactive lists) and evict */
  1752. ret = i915_gpu_idle(dev);
  1753. if (ret)
  1754. return ret;
  1755. BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
  1756. ret = i915_gem_evict_from_inactive_list(dev);
  1757. if (ret)
  1758. return ret;
  1759. spin_lock(&dev_priv->mm.active_list_lock);
  1760. lists_empty = (list_empty(&dev_priv->mm.inactive_list) &&
  1761. list_empty(&dev_priv->mm.flushing_list) &&
  1762. list_empty(&dev_priv->render_ring.active_list) &&
  1763. (!HAS_BSD(dev)
  1764. || list_empty(&dev_priv->bsd_ring.active_list)));
  1765. spin_unlock(&dev_priv->mm.active_list_lock);
  1766. BUG_ON(!lists_empty);
  1767. return 0;
  1768. }
  1769. static int
  1770. i915_gem_evict_something(struct drm_device *dev, int min_size)
  1771. {
  1772. drm_i915_private_t *dev_priv = dev->dev_private;
  1773. struct drm_gem_object *obj;
  1774. int ret;
  1775. struct intel_ring_buffer *render_ring = &dev_priv->render_ring;
  1776. struct intel_ring_buffer *bsd_ring = &dev_priv->bsd_ring;
  1777. for (;;) {
  1778. i915_gem_retire_requests(dev, render_ring);
  1779. if (HAS_BSD(dev))
  1780. i915_gem_retire_requests(dev, bsd_ring);
  1781. /* If there's an inactive buffer available now, grab it
  1782. * and be done.
  1783. */
  1784. obj = i915_gem_find_inactive_object(dev, min_size);
  1785. if (obj) {
  1786. struct drm_i915_gem_object *obj_priv;
  1787. #if WATCH_LRU
  1788. DRM_INFO("%s: evicting %p\n", __func__, obj);
  1789. #endif
  1790. obj_priv = to_intel_bo(obj);
  1791. BUG_ON(obj_priv->pin_count != 0);
  1792. BUG_ON(obj_priv->active);
  1793. /* Wait on the rendering and unbind the buffer. */
  1794. return i915_gem_object_unbind(obj);
  1795. }
  1796. /* If we didn't get anything, but the ring is still processing
  1797. * things, wait for the next to finish and hopefully leave us
  1798. * a buffer to evict.
  1799. */
  1800. if (!list_empty(&render_ring->request_list)) {
  1801. struct drm_i915_gem_request *request;
  1802. request = list_first_entry(&render_ring->request_list,
  1803. struct drm_i915_gem_request,
  1804. list);
  1805. ret = i915_wait_request(dev,
  1806. request->seqno, request->ring);
  1807. if (ret)
  1808. return ret;
  1809. continue;
  1810. }
  1811. if (HAS_BSD(dev) && !list_empty(&bsd_ring->request_list)) {
  1812. struct drm_i915_gem_request *request;
  1813. request = list_first_entry(&bsd_ring->request_list,
  1814. struct drm_i915_gem_request,
  1815. list);
  1816. ret = i915_wait_request(dev,
  1817. request->seqno, request->ring);
  1818. if (ret)
  1819. return ret;
  1820. continue;
  1821. }
  1822. /* If we didn't have anything on the request list but there
  1823. * are buffers awaiting a flush, emit one and try again.
  1824. * When we wait on it, those buffers waiting for that flush
  1825. * will get moved to inactive.
  1826. */
  1827. if (!list_empty(&dev_priv->mm.flushing_list)) {
  1828. struct drm_i915_gem_object *obj_priv;
  1829. /* Find an object that we can immediately reuse */
  1830. list_for_each_entry(obj_priv, &dev_priv->mm.flushing_list, list) {
  1831. obj = &obj_priv->base;
  1832. if (obj->size >= min_size)
  1833. break;
  1834. obj = NULL;
  1835. }
  1836. if (obj != NULL) {
  1837. uint32_t seqno;
  1838. i915_gem_flush_ring(dev,
  1839. obj->write_domain,
  1840. obj->write_domain,
  1841. obj_priv->ring);
  1842. seqno = i915_add_request(dev, NULL,
  1843. obj->write_domain,
  1844. obj_priv->ring);
  1845. if (seqno == 0)
  1846. return -ENOMEM;
  1847. continue;
  1848. }
  1849. }
  1850. /* If we didn't do any of the above, there's no single buffer
  1851. * large enough to swap out for the new one, so just evict
  1852. * everything and start again. (This should be rare.)
  1853. */
  1854. if (!list_empty (&dev_priv->mm.inactive_list))
  1855. return i915_gem_evict_from_inactive_list(dev);
  1856. else
  1857. return i915_gem_evict_everything(dev);
  1858. }
  1859. }
  1860. int
  1861. i915_gem_object_get_pages(struct drm_gem_object *obj,
  1862. gfp_t gfpmask)
  1863. {
  1864. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  1865. int page_count, i;
  1866. struct address_space *mapping;
  1867. struct inode *inode;
  1868. struct page *page;
  1869. BUG_ON(obj_priv->pages_refcount
  1870. == DRM_I915_GEM_OBJECT_MAX_PAGES_REFCOUNT);
  1871. if (obj_priv->pages_refcount++ != 0)
  1872. return 0;
  1873. /* Get the list of pages out of our struct file. They'll be pinned
  1874. * at this point until we release them.
  1875. */
  1876. page_count = obj->size / PAGE_SIZE;
  1877. BUG_ON(obj_priv->pages != NULL);
  1878. obj_priv->pages = drm_calloc_large(page_count, sizeof(struct page *));
  1879. if (obj_priv->pages == NULL) {
  1880. obj_priv->pages_refcount--;
  1881. return -ENOMEM;
  1882. }
  1883. inode = obj->filp->f_path.dentry->d_inode;
  1884. mapping = inode->i_mapping;
  1885. for (i = 0; i < page_count; i++) {
  1886. page = read_cache_page_gfp(mapping, i,
  1887. GFP_HIGHUSER |
  1888. __GFP_COLD |
  1889. __GFP_RECLAIMABLE |
  1890. gfpmask);
  1891. if (IS_ERR(page))
  1892. goto err_pages;
  1893. obj_priv->pages[i] = page;
  1894. }
  1895. if (obj_priv->tiling_mode != I915_TILING_NONE)
  1896. i915_gem_object_do_bit_17_swizzle(obj);
  1897. return 0;
  1898. err_pages:
  1899. while (i--)
  1900. page_cache_release(obj_priv->pages[i]);
  1901. drm_free_large(obj_priv->pages);
  1902. obj_priv->pages = NULL;
  1903. obj_priv->pages_refcount--;
  1904. return PTR_ERR(page);
  1905. }
  1906. static void sandybridge_write_fence_reg(struct drm_i915_fence_reg *reg)
  1907. {
  1908. struct drm_gem_object *obj = reg->obj;
  1909. struct drm_device *dev = obj->dev;
  1910. drm_i915_private_t *dev_priv = dev->dev_private;
  1911. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  1912. int regnum = obj_priv->fence_reg;
  1913. uint64_t val;
  1914. val = (uint64_t)((obj_priv->gtt_offset + obj->size - 4096) &
  1915. 0xfffff000) << 32;
  1916. val |= obj_priv->gtt_offset & 0xfffff000;
  1917. val |= (uint64_t)((obj_priv->stride / 128) - 1) <<
  1918. SANDYBRIDGE_FENCE_PITCH_SHIFT;
  1919. if (obj_priv->tiling_mode == I915_TILING_Y)
  1920. val |= 1 << I965_FENCE_TILING_Y_SHIFT;
  1921. val |= I965_FENCE_REG_VALID;
  1922. I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + (regnum * 8), val);
  1923. }
  1924. static void i965_write_fence_reg(struct drm_i915_fence_reg *reg)
  1925. {
  1926. struct drm_gem_object *obj = reg->obj;
  1927. struct drm_device *dev = obj->dev;
  1928. drm_i915_private_t *dev_priv = dev->dev_private;
  1929. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  1930. int regnum = obj_priv->fence_reg;
  1931. uint64_t val;
  1932. val = (uint64_t)((obj_priv->gtt_offset + obj->size - 4096) &
  1933. 0xfffff000) << 32;
  1934. val |= obj_priv->gtt_offset & 0xfffff000;
  1935. val |= ((obj_priv->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
  1936. if (obj_priv->tiling_mode == I915_TILING_Y)
  1937. val |= 1 << I965_FENCE_TILING_Y_SHIFT;
  1938. val |= I965_FENCE_REG_VALID;
  1939. I915_WRITE64(FENCE_REG_965_0 + (regnum * 8), val);
  1940. }
  1941. static void i915_write_fence_reg(struct drm_i915_fence_reg *reg)
  1942. {
  1943. struct drm_gem_object *obj = reg->obj;
  1944. struct drm_device *dev = obj->dev;
  1945. drm_i915_private_t *dev_priv = dev->dev_private;
  1946. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  1947. int regnum = obj_priv->fence_reg;
  1948. int tile_width;
  1949. uint32_t fence_reg, val;
  1950. uint32_t pitch_val;
  1951. if ((obj_priv->gtt_offset & ~I915_FENCE_START_MASK) ||
  1952. (obj_priv->gtt_offset & (obj->size - 1))) {
  1953. WARN(1, "%s: object 0x%08x not 1M or size (0x%zx) aligned\n",
  1954. __func__, obj_priv->gtt_offset, obj->size);
  1955. return;
  1956. }
  1957. if (obj_priv->tiling_mode == I915_TILING_Y &&
  1958. HAS_128_BYTE_Y_TILING(dev))
  1959. tile_width = 128;
  1960. else
  1961. tile_width = 512;
  1962. /* Note: pitch better be a power of two tile widths */
  1963. pitch_val = obj_priv->stride / tile_width;
  1964. pitch_val = ffs(pitch_val) - 1;
  1965. if (obj_priv->tiling_mode == I915_TILING_Y &&
  1966. HAS_128_BYTE_Y_TILING(dev))
  1967. WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
  1968. else
  1969. WARN_ON(pitch_val > I915_FENCE_MAX_PITCH_VAL);
  1970. val = obj_priv->gtt_offset;
  1971. if (obj_priv->tiling_mode == I915_TILING_Y)
  1972. val |= 1 << I830_FENCE_TILING_Y_SHIFT;
  1973. val |= I915_FENCE_SIZE_BITS(obj->size);
  1974. val |= pitch_val << I830_FENCE_PITCH_SHIFT;
  1975. val |= I830_FENCE_REG_VALID;
  1976. if (regnum < 8)
  1977. fence_reg = FENCE_REG_830_0 + (regnum * 4);
  1978. else
  1979. fence_reg = FENCE_REG_945_8 + ((regnum - 8) * 4);
  1980. I915_WRITE(fence_reg, val);
  1981. }
  1982. static void i830_write_fence_reg(struct drm_i915_fence_reg *reg)
  1983. {
  1984. struct drm_gem_object *obj = reg->obj;
  1985. struct drm_device *dev = obj->dev;
  1986. drm_i915_private_t *dev_priv = dev->dev_private;
  1987. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  1988. int regnum = obj_priv->fence_reg;
  1989. uint32_t val;
  1990. uint32_t pitch_val;
  1991. uint32_t fence_size_bits;
  1992. if ((obj_priv->gtt_offset & ~I830_FENCE_START_MASK) ||
  1993. (obj_priv->gtt_offset & (obj->size - 1))) {
  1994. WARN(1, "%s: object 0x%08x not 512K or size aligned\n",
  1995. __func__, obj_priv->gtt_offset);
  1996. return;
  1997. }
  1998. pitch_val = obj_priv->stride / 128;
  1999. pitch_val = ffs(pitch_val) - 1;
  2000. WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
  2001. val = obj_priv->gtt_offset;
  2002. if (obj_priv->tiling_mode == I915_TILING_Y)
  2003. val |= 1 << I830_FENCE_TILING_Y_SHIFT;
  2004. fence_size_bits = I830_FENCE_SIZE_BITS(obj->size);
  2005. WARN_ON(fence_size_bits & ~0x00000f00);
  2006. val |= fence_size_bits;
  2007. val |= pitch_val << I830_FENCE_PITCH_SHIFT;
  2008. val |= I830_FENCE_REG_VALID;
  2009. I915_WRITE(FENCE_REG_830_0 + (regnum * 4), val);
  2010. }
  2011. static int i915_find_fence_reg(struct drm_device *dev)
  2012. {
  2013. struct drm_i915_fence_reg *reg = NULL;
  2014. struct drm_i915_gem_object *obj_priv = NULL;
  2015. struct drm_i915_private *dev_priv = dev->dev_private;
  2016. struct drm_gem_object *obj = NULL;
  2017. int i, avail, ret;
  2018. /* First try to find a free reg */
  2019. avail = 0;
  2020. for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
  2021. reg = &dev_priv->fence_regs[i];
  2022. if (!reg->obj)
  2023. return i;
  2024. obj_priv = to_intel_bo(reg->obj);
  2025. if (!obj_priv->pin_count)
  2026. avail++;
  2027. }
  2028. if (avail == 0)
  2029. return -ENOSPC;
  2030. /* None available, try to steal one or wait for a user to finish */
  2031. i = I915_FENCE_REG_NONE;
  2032. list_for_each_entry(reg, &dev_priv->mm.fence_list,
  2033. lru_list) {
  2034. obj = reg->obj;
  2035. obj_priv = to_intel_bo(obj);
  2036. if (obj_priv->pin_count)
  2037. continue;
  2038. /* found one! */
  2039. i = obj_priv->fence_reg;
  2040. break;
  2041. }
  2042. BUG_ON(i == I915_FENCE_REG_NONE);
  2043. /* We only have a reference on obj from the active list. put_fence_reg
  2044. * might drop that one, causing a use-after-free in it. So hold a
  2045. * private reference to obj like the other callers of put_fence_reg
  2046. * (set_tiling ioctl) do. */
  2047. drm_gem_object_reference(obj);
  2048. ret = i915_gem_object_put_fence_reg(obj);
  2049. drm_gem_object_unreference(obj);
  2050. if (ret != 0)
  2051. return ret;
  2052. return i;
  2053. }
  2054. /**
  2055. * i915_gem_object_get_fence_reg - set up a fence reg for an object
  2056. * @obj: object to map through a fence reg
  2057. *
  2058. * When mapping objects through the GTT, userspace wants to be able to write
  2059. * to them without having to worry about swizzling if the object is tiled.
  2060. *
  2061. * This function walks the fence regs looking for a free one for @obj,
  2062. * stealing one if it can't find any.
  2063. *
  2064. * It then sets up the reg based on the object's properties: address, pitch
  2065. * and tiling format.
  2066. */
  2067. int
  2068. i915_gem_object_get_fence_reg(struct drm_gem_object *obj)
  2069. {
  2070. struct drm_device *dev = obj->dev;
  2071. struct drm_i915_private *dev_priv = dev->dev_private;
  2072. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  2073. struct drm_i915_fence_reg *reg = NULL;
  2074. int ret;
  2075. /* Just update our place in the LRU if our fence is getting used. */
  2076. if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
  2077. reg = &dev_priv->fence_regs[obj_priv->fence_reg];
  2078. list_move_tail(&reg->lru_list, &dev_priv->mm.fence_list);
  2079. return 0;
  2080. }
  2081. switch (obj_priv->tiling_mode) {
  2082. case I915_TILING_NONE:
  2083. WARN(1, "allocating a fence for non-tiled object?\n");
  2084. break;
  2085. case I915_TILING_X:
  2086. if (!obj_priv->stride)
  2087. return -EINVAL;
  2088. WARN((obj_priv->stride & (512 - 1)),
  2089. "object 0x%08x is X tiled but has non-512B pitch\n",
  2090. obj_priv->gtt_offset);
  2091. break;
  2092. case I915_TILING_Y:
  2093. if (!obj_priv->stride)
  2094. return -EINVAL;
  2095. WARN((obj_priv->stride & (128 - 1)),
  2096. "object 0x%08x is Y tiled but has non-128B pitch\n",
  2097. obj_priv->gtt_offset);
  2098. break;
  2099. }
  2100. ret = i915_find_fence_reg(dev);
  2101. if (ret < 0)
  2102. return ret;
  2103. obj_priv->fence_reg = ret;
  2104. reg = &dev_priv->fence_regs[obj_priv->fence_reg];
  2105. list_add_tail(&reg->lru_list, &dev_priv->mm.fence_list);
  2106. reg->obj = obj;
  2107. if (IS_GEN6(dev))
  2108. sandybridge_write_fence_reg(reg);
  2109. else if (IS_I965G(dev))
  2110. i965_write_fence_reg(reg);
  2111. else if (IS_I9XX(dev))
  2112. i915_write_fence_reg(reg);
  2113. else
  2114. i830_write_fence_reg(reg);
  2115. trace_i915_gem_object_get_fence(obj, obj_priv->fence_reg,
  2116. obj_priv->tiling_mode);
  2117. return 0;
  2118. }
  2119. /**
  2120. * i915_gem_clear_fence_reg - clear out fence register info
  2121. * @obj: object to clear
  2122. *
  2123. * Zeroes out the fence register itself and clears out the associated
  2124. * data structures in dev_priv and obj_priv.
  2125. */
  2126. static void
  2127. i915_gem_clear_fence_reg(struct drm_gem_object *obj)
  2128. {
  2129. struct drm_device *dev = obj->dev;
  2130. drm_i915_private_t *dev_priv = dev->dev_private;
  2131. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  2132. struct drm_i915_fence_reg *reg =
  2133. &dev_priv->fence_regs[obj_priv->fence_reg];
  2134. if (IS_GEN6(dev)) {
  2135. I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 +
  2136. (obj_priv->fence_reg * 8), 0);
  2137. } else if (IS_I965G(dev)) {
  2138. I915_WRITE64(FENCE_REG_965_0 + (obj_priv->fence_reg * 8), 0);
  2139. } else {
  2140. uint32_t fence_reg;
  2141. if (obj_priv->fence_reg < 8)
  2142. fence_reg = FENCE_REG_830_0 + obj_priv->fence_reg * 4;
  2143. else
  2144. fence_reg = FENCE_REG_945_8 + (obj_priv->fence_reg -
  2145. 8) * 4;
  2146. I915_WRITE(fence_reg, 0);
  2147. }
  2148. reg->obj = NULL;
  2149. obj_priv->fence_reg = I915_FENCE_REG_NONE;
  2150. list_del_init(&reg->lru_list);
  2151. }
  2152. /**
  2153. * i915_gem_object_put_fence_reg - waits on outstanding fenced access
  2154. * to the buffer to finish, and then resets the fence register.
  2155. * @obj: tiled object holding a fence register.
  2156. *
  2157. * Zeroes out the fence register itself and clears out the associated
  2158. * data structures in dev_priv and obj_priv.
  2159. */
  2160. int
  2161. i915_gem_object_put_fence_reg(struct drm_gem_object *obj)
  2162. {
  2163. struct drm_device *dev = obj->dev;
  2164. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  2165. if (obj_priv->fence_reg == I915_FENCE_REG_NONE)
  2166. return 0;
  2167. /* If we've changed tiling, GTT-mappings of the object
  2168. * need to re-fault to ensure that the correct fence register
  2169. * setup is in place.
  2170. */
  2171. i915_gem_release_mmap(obj);
  2172. /* On the i915, GPU access to tiled buffers is via a fence,
  2173. * therefore we must wait for any outstanding access to complete
  2174. * before clearing the fence.
  2175. */
  2176. if (!IS_I965G(dev)) {
  2177. int ret;
  2178. i915_gem_object_flush_gpu_write_domain(obj);
  2179. ret = i915_gem_object_wait_rendering(obj);
  2180. if (ret != 0)
  2181. return ret;
  2182. }
  2183. i915_gem_object_flush_gtt_write_domain(obj);
  2184. i915_gem_clear_fence_reg (obj);
  2185. return 0;
  2186. }
  2187. /**
  2188. * Finds free space in the GTT aperture and binds the object there.
  2189. */
  2190. static int
  2191. i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment)
  2192. {
  2193. struct drm_device *dev = obj->dev;
  2194. drm_i915_private_t *dev_priv = dev->dev_private;
  2195. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  2196. struct drm_mm_node *free_space;
  2197. gfp_t gfpmask = __GFP_NORETRY | __GFP_NOWARN;
  2198. int ret;
  2199. if (obj_priv->madv != I915_MADV_WILLNEED) {
  2200. DRM_ERROR("Attempting to bind a purgeable object\n");
  2201. return -EINVAL;
  2202. }
  2203. if (alignment == 0)
  2204. alignment = i915_gem_get_gtt_alignment(obj);
  2205. if (alignment & (i915_gem_get_gtt_alignment(obj) - 1)) {
  2206. DRM_ERROR("Invalid object alignment requested %u\n", alignment);
  2207. return -EINVAL;
  2208. }
  2209. /* If the object is bigger than the entire aperture, reject it early
  2210. * before evicting everything in a vain attempt to find space.
  2211. */
  2212. if (obj->size > dev->gtt_total) {
  2213. DRM_ERROR("Attempting to bind an object larger than the aperture\n");
  2214. return -E2BIG;
  2215. }
  2216. search_free:
  2217. free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
  2218. obj->size, alignment, 0);
  2219. if (free_space != NULL) {
  2220. obj_priv->gtt_space = drm_mm_get_block(free_space, obj->size,
  2221. alignment);
  2222. if (obj_priv->gtt_space != NULL)
  2223. obj_priv->gtt_offset = obj_priv->gtt_space->start;
  2224. }
  2225. if (obj_priv->gtt_space == NULL) {
  2226. /* If the gtt is empty and we're still having trouble
  2227. * fitting our object in, we're out of memory.
  2228. */
  2229. #if WATCH_LRU
  2230. DRM_INFO("%s: GTT full, evicting something\n", __func__);
  2231. #endif
  2232. ret = i915_gem_evict_something(dev, obj->size);
  2233. if (ret)
  2234. return ret;
  2235. goto search_free;
  2236. }
  2237. #if WATCH_BUF
  2238. DRM_INFO("Binding object of size %zd at 0x%08x\n",
  2239. obj->size, obj_priv->gtt_offset);
  2240. #endif
  2241. ret = i915_gem_object_get_pages(obj, gfpmask);
  2242. if (ret) {
  2243. drm_mm_put_block(obj_priv->gtt_space);
  2244. obj_priv->gtt_space = NULL;
  2245. if (ret == -ENOMEM) {
  2246. /* first try to clear up some space from the GTT */
  2247. ret = i915_gem_evict_something(dev, obj->size);
  2248. if (ret) {
  2249. /* now try to shrink everyone else */
  2250. if (gfpmask) {
  2251. gfpmask = 0;
  2252. goto search_free;
  2253. }
  2254. return ret;
  2255. }
  2256. goto search_free;
  2257. }
  2258. return ret;
  2259. }
  2260. /* Create an AGP memory structure pointing at our pages, and bind it
  2261. * into the GTT.
  2262. */
  2263. obj_priv->agp_mem = drm_agp_bind_pages(dev,
  2264. obj_priv->pages,
  2265. obj->size >> PAGE_SHIFT,
  2266. obj_priv->gtt_offset,
  2267. obj_priv->agp_type);
  2268. if (obj_priv->agp_mem == NULL) {
  2269. i915_gem_object_put_pages(obj);
  2270. drm_mm_put_block(obj_priv->gtt_space);
  2271. obj_priv->gtt_space = NULL;
  2272. ret = i915_gem_evict_something(dev, obj->size);
  2273. if (ret)
  2274. return ret;
  2275. goto search_free;
  2276. }
  2277. atomic_inc(&dev->gtt_count);
  2278. atomic_add(obj->size, &dev->gtt_memory);
  2279. /* Assert that the object is not currently in any GPU domain. As it
  2280. * wasn't in the GTT, there shouldn't be any way it could have been in
  2281. * a GPU cache
  2282. */
  2283. BUG_ON(obj->read_domains & I915_GEM_GPU_DOMAINS);
  2284. BUG_ON(obj->write_domain & I915_GEM_GPU_DOMAINS);
  2285. trace_i915_gem_object_bind(obj, obj_priv->gtt_offset);
  2286. return 0;
  2287. }
  2288. void
  2289. i915_gem_clflush_object(struct drm_gem_object *obj)
  2290. {
  2291. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  2292. /* If we don't have a page list set up, then we're not pinned
  2293. * to GPU, and we can ignore the cache flush because it'll happen
  2294. * again at bind time.
  2295. */
  2296. if (obj_priv->pages == NULL)
  2297. return;
  2298. trace_i915_gem_object_clflush(obj);
  2299. drm_clflush_pages(obj_priv->pages, obj->size / PAGE_SIZE);
  2300. }
  2301. /** Flushes any GPU write domain for the object if it's dirty. */
  2302. static void
  2303. i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj)
  2304. {
  2305. struct drm_device *dev = obj->dev;
  2306. uint32_t old_write_domain;
  2307. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  2308. if ((obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
  2309. return;
  2310. /* Queue the GPU write cache flushing we need. */
  2311. old_write_domain = obj->write_domain;
  2312. i915_gem_flush(dev, 0, obj->write_domain);
  2313. (void) i915_add_request(dev, NULL, obj->write_domain, obj_priv->ring);
  2314. BUG_ON(obj->write_domain);
  2315. trace_i915_gem_object_change_domain(obj,
  2316. obj->read_domains,
  2317. old_write_domain);
  2318. }
  2319. /** Flushes the GTT write domain for the object if it's dirty. */
  2320. static void
  2321. i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj)
  2322. {
  2323. uint32_t old_write_domain;
  2324. if (obj->write_domain != I915_GEM_DOMAIN_GTT)
  2325. return;
  2326. /* No actual flushing is required for the GTT write domain. Writes
  2327. * to it immediately go to main memory as far as we know, so there's
  2328. * no chipset flush. It also doesn't land in render cache.
  2329. */
  2330. old_write_domain = obj->write_domain;
  2331. obj->write_domain = 0;
  2332. trace_i915_gem_object_change_domain(obj,
  2333. obj->read_domains,
  2334. old_write_domain);
  2335. }
  2336. /** Flushes the CPU write domain for the object if it's dirty. */
  2337. static void
  2338. i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj)
  2339. {
  2340. struct drm_device *dev = obj->dev;
  2341. uint32_t old_write_domain;
  2342. if (obj->write_domain != I915_GEM_DOMAIN_CPU)
  2343. return;
  2344. i915_gem_clflush_object(obj);
  2345. drm_agp_chipset_flush(dev);
  2346. old_write_domain = obj->write_domain;
  2347. obj->write_domain = 0;
  2348. trace_i915_gem_object_change_domain(obj,
  2349. obj->read_domains,
  2350. old_write_domain);
  2351. }
  2352. void
  2353. i915_gem_object_flush_write_domain(struct drm_gem_object *obj)
  2354. {
  2355. switch (obj->write_domain) {
  2356. case I915_GEM_DOMAIN_GTT:
  2357. i915_gem_object_flush_gtt_write_domain(obj);
  2358. break;
  2359. case I915_GEM_DOMAIN_CPU:
  2360. i915_gem_object_flush_cpu_write_domain(obj);
  2361. break;
  2362. default:
  2363. i915_gem_object_flush_gpu_write_domain(obj);
  2364. break;
  2365. }
  2366. }
  2367. /**
  2368. * Moves a single object to the GTT read, and possibly write domain.
  2369. *
  2370. * This function returns when the move is complete, including waiting on
  2371. * flushes to occur.
  2372. */
  2373. int
  2374. i915_gem_object_set_to_gtt_domain(struct drm_gem_object *obj, int write)
  2375. {
  2376. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  2377. uint32_t old_write_domain, old_read_domains;
  2378. int ret;
  2379. /* Not valid to be called on unbound objects. */
  2380. if (obj_priv->gtt_space == NULL)
  2381. return -EINVAL;
  2382. i915_gem_object_flush_gpu_write_domain(obj);
  2383. /* Wait on any GPU rendering and flushing to occur. */
  2384. ret = i915_gem_object_wait_rendering(obj);
  2385. if (ret != 0)
  2386. return ret;
  2387. old_write_domain = obj->write_domain;
  2388. old_read_domains = obj->read_domains;
  2389. /* If we're writing through the GTT domain, then CPU and GPU caches
  2390. * will need to be invalidated at next use.
  2391. */
  2392. if (write)
  2393. obj->read_domains &= I915_GEM_DOMAIN_GTT;
  2394. i915_gem_object_flush_cpu_write_domain(obj);
  2395. /* It should now be out of any other write domains, and we can update
  2396. * the domain values for our changes.
  2397. */
  2398. BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
  2399. obj->read_domains |= I915_GEM_DOMAIN_GTT;
  2400. if (write) {
  2401. obj->write_domain = I915_GEM_DOMAIN_GTT;
  2402. obj_priv->dirty = 1;
  2403. }
  2404. trace_i915_gem_object_change_domain(obj,
  2405. old_read_domains,
  2406. old_write_domain);
  2407. return 0;
  2408. }
  2409. /*
  2410. * Prepare buffer for display plane. Use uninterruptible for possible flush
  2411. * wait, as in modesetting process we're not supposed to be interrupted.
  2412. */
  2413. int
  2414. i915_gem_object_set_to_display_plane(struct drm_gem_object *obj)
  2415. {
  2416. struct drm_device *dev = obj->dev;
  2417. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  2418. uint32_t old_write_domain, old_read_domains;
  2419. int ret;
  2420. /* Not valid to be called on unbound objects. */
  2421. if (obj_priv->gtt_space == NULL)
  2422. return -EINVAL;
  2423. i915_gem_object_flush_gpu_write_domain(obj);
  2424. /* Wait on any GPU rendering and flushing to occur. */
  2425. if (obj_priv->active) {
  2426. #if WATCH_BUF
  2427. DRM_INFO("%s: object %p wait for seqno %08x\n",
  2428. __func__, obj, obj_priv->last_rendering_seqno);
  2429. #endif
  2430. ret = i915_do_wait_request(dev,
  2431. obj_priv->last_rendering_seqno,
  2432. 0,
  2433. obj_priv->ring);
  2434. if (ret != 0)
  2435. return ret;
  2436. }
  2437. i915_gem_object_flush_cpu_write_domain(obj);
  2438. old_write_domain = obj->write_domain;
  2439. old_read_domains = obj->read_domains;
  2440. /* It should now be out of any other write domains, and we can update
  2441. * the domain values for our changes.
  2442. */
  2443. BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
  2444. obj->read_domains = I915_GEM_DOMAIN_GTT;
  2445. obj->write_domain = I915_GEM_DOMAIN_GTT;
  2446. obj_priv->dirty = 1;
  2447. trace_i915_gem_object_change_domain(obj,
  2448. old_read_domains,
  2449. old_write_domain);
  2450. return 0;
  2451. }
  2452. /**
  2453. * Moves a single object to the CPU read, and possibly write domain.
  2454. *
  2455. * This function returns when the move is complete, including waiting on
  2456. * flushes to occur.
  2457. */
  2458. static int
  2459. i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj, int write)
  2460. {
  2461. uint32_t old_write_domain, old_read_domains;
  2462. int ret;
  2463. i915_gem_object_flush_gpu_write_domain(obj);
  2464. /* Wait on any GPU rendering and flushing to occur. */
  2465. ret = i915_gem_object_wait_rendering(obj);
  2466. if (ret != 0)
  2467. return ret;
  2468. i915_gem_object_flush_gtt_write_domain(obj);
  2469. /* If we have a partially-valid cache of the object in the CPU,
  2470. * finish invalidating it and free the per-page flags.
  2471. */
  2472. i915_gem_object_set_to_full_cpu_read_domain(obj);
  2473. old_write_domain = obj->write_domain;
  2474. old_read_domains = obj->read_domains;
  2475. /* Flush the CPU cache if it's still invalid. */
  2476. if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) {
  2477. i915_gem_clflush_object(obj);
  2478. obj->read_domains |= I915_GEM_DOMAIN_CPU;
  2479. }
  2480. /* It should now be out of any other write domains, and we can update
  2481. * the domain values for our changes.
  2482. */
  2483. BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
  2484. /* If we're writing through the CPU, then the GPU read domains will
  2485. * need to be invalidated at next use.
  2486. */
  2487. if (write) {
  2488. obj->read_domains &= I915_GEM_DOMAIN_CPU;
  2489. obj->write_domain = I915_GEM_DOMAIN_CPU;
  2490. }
  2491. trace_i915_gem_object_change_domain(obj,
  2492. old_read_domains,
  2493. old_write_domain);
  2494. return 0;
  2495. }
  2496. /*
  2497. * Set the next domain for the specified object. This
  2498. * may not actually perform the necessary flushing/invaliding though,
  2499. * as that may want to be batched with other set_domain operations
  2500. *
  2501. * This is (we hope) the only really tricky part of gem. The goal
  2502. * is fairly simple -- track which caches hold bits of the object
  2503. * and make sure they remain coherent. A few concrete examples may
  2504. * help to explain how it works. For shorthand, we use the notation
  2505. * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the
  2506. * a pair of read and write domain masks.
  2507. *
  2508. * Case 1: the batch buffer
  2509. *
  2510. * 1. Allocated
  2511. * 2. Written by CPU
  2512. * 3. Mapped to GTT
  2513. * 4. Read by GPU
  2514. * 5. Unmapped from GTT
  2515. * 6. Freed
  2516. *
  2517. * Let's take these a step at a time
  2518. *
  2519. * 1. Allocated
  2520. * Pages allocated from the kernel may still have
  2521. * cache contents, so we set them to (CPU, CPU) always.
  2522. * 2. Written by CPU (using pwrite)
  2523. * The pwrite function calls set_domain (CPU, CPU) and
  2524. * this function does nothing (as nothing changes)
  2525. * 3. Mapped by GTT
  2526. * This function asserts that the object is not
  2527. * currently in any GPU-based read or write domains
  2528. * 4. Read by GPU
  2529. * i915_gem_execbuffer calls set_domain (COMMAND, 0).
  2530. * As write_domain is zero, this function adds in the
  2531. * current read domains (CPU+COMMAND, 0).
  2532. * flush_domains is set to CPU.
  2533. * invalidate_domains is set to COMMAND
  2534. * clflush is run to get data out of the CPU caches
  2535. * then i915_dev_set_domain calls i915_gem_flush to
  2536. * emit an MI_FLUSH and drm_agp_chipset_flush
  2537. * 5. Unmapped from GTT
  2538. * i915_gem_object_unbind calls set_domain (CPU, CPU)
  2539. * flush_domains and invalidate_domains end up both zero
  2540. * so no flushing/invalidating happens
  2541. * 6. Freed
  2542. * yay, done
  2543. *
  2544. * Case 2: The shared render buffer
  2545. *
  2546. * 1. Allocated
  2547. * 2. Mapped to GTT
  2548. * 3. Read/written by GPU
  2549. * 4. set_domain to (CPU,CPU)
  2550. * 5. Read/written by CPU
  2551. * 6. Read/written by GPU
  2552. *
  2553. * 1. Allocated
  2554. * Same as last example, (CPU, CPU)
  2555. * 2. Mapped to GTT
  2556. * Nothing changes (assertions find that it is not in the GPU)
  2557. * 3. Read/written by GPU
  2558. * execbuffer calls set_domain (RENDER, RENDER)
  2559. * flush_domains gets CPU
  2560. * invalidate_domains gets GPU
  2561. * clflush (obj)
  2562. * MI_FLUSH and drm_agp_chipset_flush
  2563. * 4. set_domain (CPU, CPU)
  2564. * flush_domains gets GPU
  2565. * invalidate_domains gets CPU
  2566. * wait_rendering (obj) to make sure all drawing is complete.
  2567. * This will include an MI_FLUSH to get the data from GPU
  2568. * to memory
  2569. * clflush (obj) to invalidate the CPU cache
  2570. * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?)
  2571. * 5. Read/written by CPU
  2572. * cache lines are loaded and dirtied
  2573. * 6. Read written by GPU
  2574. * Same as last GPU access
  2575. *
  2576. * Case 3: The constant buffer
  2577. *
  2578. * 1. Allocated
  2579. * 2. Written by CPU
  2580. * 3. Read by GPU
  2581. * 4. Updated (written) by CPU again
  2582. * 5. Read by GPU
  2583. *
  2584. * 1. Allocated
  2585. * (CPU, CPU)
  2586. * 2. Written by CPU
  2587. * (CPU, CPU)
  2588. * 3. Read by GPU
  2589. * (CPU+RENDER, 0)
  2590. * flush_domains = CPU
  2591. * invalidate_domains = RENDER
  2592. * clflush (obj)
  2593. * MI_FLUSH
  2594. * drm_agp_chipset_flush
  2595. * 4. Updated (written) by CPU again
  2596. * (CPU, CPU)
  2597. * flush_domains = 0 (no previous write domain)
  2598. * invalidate_domains = 0 (no new read domains)
  2599. * 5. Read by GPU
  2600. * (CPU+RENDER, 0)
  2601. * flush_domains = CPU
  2602. * invalidate_domains = RENDER
  2603. * clflush (obj)
  2604. * MI_FLUSH
  2605. * drm_agp_chipset_flush
  2606. */
  2607. static void
  2608. i915_gem_object_set_to_gpu_domain(struct drm_gem_object *obj)
  2609. {
  2610. struct drm_device *dev = obj->dev;
  2611. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  2612. uint32_t invalidate_domains = 0;
  2613. uint32_t flush_domains = 0;
  2614. uint32_t old_read_domains;
  2615. BUG_ON(obj->pending_read_domains & I915_GEM_DOMAIN_CPU);
  2616. BUG_ON(obj->pending_write_domain == I915_GEM_DOMAIN_CPU);
  2617. intel_mark_busy(dev, obj);
  2618. #if WATCH_BUF
  2619. DRM_INFO("%s: object %p read %08x -> %08x write %08x -> %08x\n",
  2620. __func__, obj,
  2621. obj->read_domains, obj->pending_read_domains,
  2622. obj->write_domain, obj->pending_write_domain);
  2623. #endif
  2624. /*
  2625. * If the object isn't moving to a new write domain,
  2626. * let the object stay in multiple read domains
  2627. */
  2628. if (obj->pending_write_domain == 0)
  2629. obj->pending_read_domains |= obj->read_domains;
  2630. else
  2631. obj_priv->dirty = 1;
  2632. /*
  2633. * Flush the current write domain if
  2634. * the new read domains don't match. Invalidate
  2635. * any read domains which differ from the old
  2636. * write domain
  2637. */
  2638. if (obj->write_domain &&
  2639. obj->write_domain != obj->pending_read_domains) {
  2640. flush_domains |= obj->write_domain;
  2641. invalidate_domains |=
  2642. obj->pending_read_domains & ~obj->write_domain;
  2643. }
  2644. /*
  2645. * Invalidate any read caches which may have
  2646. * stale data. That is, any new read domains.
  2647. */
  2648. invalidate_domains |= obj->pending_read_domains & ~obj->read_domains;
  2649. if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU) {
  2650. #if WATCH_BUF
  2651. DRM_INFO("%s: CPU domain flush %08x invalidate %08x\n",
  2652. __func__, flush_domains, invalidate_domains);
  2653. #endif
  2654. i915_gem_clflush_object(obj);
  2655. }
  2656. old_read_domains = obj->read_domains;
  2657. /* The actual obj->write_domain will be updated with
  2658. * pending_write_domain after we emit the accumulated flush for all
  2659. * of our domain changes in execbuffers (which clears objects'
  2660. * write_domains). So if we have a current write domain that we
  2661. * aren't changing, set pending_write_domain to that.
  2662. */
  2663. if (flush_domains == 0 && obj->pending_write_domain == 0)
  2664. obj->pending_write_domain = obj->write_domain;
  2665. obj->read_domains = obj->pending_read_domains;
  2666. dev->invalidate_domains |= invalidate_domains;
  2667. dev->flush_domains |= flush_domains;
  2668. #if WATCH_BUF
  2669. DRM_INFO("%s: read %08x write %08x invalidate %08x flush %08x\n",
  2670. __func__,
  2671. obj->read_domains, obj->write_domain,
  2672. dev->invalidate_domains, dev->flush_domains);
  2673. #endif
  2674. trace_i915_gem_object_change_domain(obj,
  2675. old_read_domains,
  2676. obj->write_domain);
  2677. }
  2678. /**
  2679. * Moves the object from a partially CPU read to a full one.
  2680. *
  2681. * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(),
  2682. * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU).
  2683. */
  2684. static void
  2685. i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj)
  2686. {
  2687. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  2688. if (!obj_priv->page_cpu_valid)
  2689. return;
  2690. /* If we're partially in the CPU read domain, finish moving it in.
  2691. */
  2692. if (obj->read_domains & I915_GEM_DOMAIN_CPU) {
  2693. int i;
  2694. for (i = 0; i <= (obj->size - 1) / PAGE_SIZE; i++) {
  2695. if (obj_priv->page_cpu_valid[i])
  2696. continue;
  2697. drm_clflush_pages(obj_priv->pages + i, 1);
  2698. }
  2699. }
  2700. /* Free the page_cpu_valid mappings which are now stale, whether
  2701. * or not we've got I915_GEM_DOMAIN_CPU.
  2702. */
  2703. kfree(obj_priv->page_cpu_valid);
  2704. obj_priv->page_cpu_valid = NULL;
  2705. }
  2706. /**
  2707. * Set the CPU read domain on a range of the object.
  2708. *
  2709. * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's
  2710. * not entirely valid. The page_cpu_valid member of the object flags which
  2711. * pages have been flushed, and will be respected by
  2712. * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping
  2713. * of the whole object.
  2714. *
  2715. * This function returns when the move is complete, including waiting on
  2716. * flushes to occur.
  2717. */
  2718. static int
  2719. i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
  2720. uint64_t offset, uint64_t size)
  2721. {
  2722. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  2723. uint32_t old_read_domains;
  2724. int i, ret;
  2725. if (offset == 0 && size == obj->size)
  2726. return i915_gem_object_set_to_cpu_domain(obj, 0);
  2727. i915_gem_object_flush_gpu_write_domain(obj);
  2728. /* Wait on any GPU rendering and flushing to occur. */
  2729. ret = i915_gem_object_wait_rendering(obj);
  2730. if (ret != 0)
  2731. return ret;
  2732. i915_gem_object_flush_gtt_write_domain(obj);
  2733. /* If we're already fully in the CPU read domain, we're done. */
  2734. if (obj_priv->page_cpu_valid == NULL &&
  2735. (obj->read_domains & I915_GEM_DOMAIN_CPU) != 0)
  2736. return 0;
  2737. /* Otherwise, create/clear the per-page CPU read domain flag if we're
  2738. * newly adding I915_GEM_DOMAIN_CPU
  2739. */
  2740. if (obj_priv->page_cpu_valid == NULL) {
  2741. obj_priv->page_cpu_valid = kzalloc(obj->size / PAGE_SIZE,
  2742. GFP_KERNEL);
  2743. if (obj_priv->page_cpu_valid == NULL)
  2744. return -ENOMEM;
  2745. } else if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0)
  2746. memset(obj_priv->page_cpu_valid, 0, obj->size / PAGE_SIZE);
  2747. /* Flush the cache on any pages that are still invalid from the CPU's
  2748. * perspective.
  2749. */
  2750. for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE;
  2751. i++) {
  2752. if (obj_priv->page_cpu_valid[i])
  2753. continue;
  2754. drm_clflush_pages(obj_priv->pages + i, 1);
  2755. obj_priv->page_cpu_valid[i] = 1;
  2756. }
  2757. /* It should now be out of any other write domains, and we can update
  2758. * the domain values for our changes.
  2759. */
  2760. BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
  2761. old_read_domains = obj->read_domains;
  2762. obj->read_domains |= I915_GEM_DOMAIN_CPU;
  2763. trace_i915_gem_object_change_domain(obj,
  2764. old_read_domains,
  2765. obj->write_domain);
  2766. return 0;
  2767. }
  2768. /**
  2769. * Pin an object to the GTT and evaluate the relocations landing in it.
  2770. */
  2771. static int
  2772. i915_gem_object_pin_and_relocate(struct drm_gem_object *obj,
  2773. struct drm_file *file_priv,
  2774. struct drm_i915_gem_exec_object2 *entry,
  2775. struct drm_i915_gem_relocation_entry *relocs)
  2776. {
  2777. struct drm_device *dev = obj->dev;
  2778. drm_i915_private_t *dev_priv = dev->dev_private;
  2779. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  2780. int i, ret;
  2781. void __iomem *reloc_page;
  2782. bool need_fence;
  2783. need_fence = entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
  2784. obj_priv->tiling_mode != I915_TILING_NONE;
  2785. /* Check fence reg constraints and rebind if necessary */
  2786. if (need_fence &&
  2787. !i915_gem_object_fence_offset_ok(obj,
  2788. obj_priv->tiling_mode)) {
  2789. ret = i915_gem_object_unbind(obj);
  2790. if (ret)
  2791. return ret;
  2792. }
  2793. /* Choose the GTT offset for our buffer and put it there. */
  2794. ret = i915_gem_object_pin(obj, (uint32_t) entry->alignment);
  2795. if (ret)
  2796. return ret;
  2797. /*
  2798. * Pre-965 chips need a fence register set up in order to
  2799. * properly handle blits to/from tiled surfaces.
  2800. */
  2801. if (need_fence) {
  2802. ret = i915_gem_object_get_fence_reg(obj);
  2803. if (ret != 0) {
  2804. i915_gem_object_unpin(obj);
  2805. return ret;
  2806. }
  2807. }
  2808. entry->offset = obj_priv->gtt_offset;
  2809. /* Apply the relocations, using the GTT aperture to avoid cache
  2810. * flushing requirements.
  2811. */
  2812. for (i = 0; i < entry->relocation_count; i++) {
  2813. struct drm_i915_gem_relocation_entry *reloc= &relocs[i];
  2814. struct drm_gem_object *target_obj;
  2815. struct drm_i915_gem_object *target_obj_priv;
  2816. uint32_t reloc_val, reloc_offset;
  2817. uint32_t __iomem *reloc_entry;
  2818. target_obj = drm_gem_object_lookup(obj->dev, file_priv,
  2819. reloc->target_handle);
  2820. if (target_obj == NULL) {
  2821. i915_gem_object_unpin(obj);
  2822. return -EBADF;
  2823. }
  2824. target_obj_priv = to_intel_bo(target_obj);
  2825. #if WATCH_RELOC
  2826. DRM_INFO("%s: obj %p offset %08x target %d "
  2827. "read %08x write %08x gtt %08x "
  2828. "presumed %08x delta %08x\n",
  2829. __func__,
  2830. obj,
  2831. (int) reloc->offset,
  2832. (int) reloc->target_handle,
  2833. (int) reloc->read_domains,
  2834. (int) reloc->write_domain,
  2835. (int) target_obj_priv->gtt_offset,
  2836. (int) reloc->presumed_offset,
  2837. reloc->delta);
  2838. #endif
  2839. /* The target buffer should have appeared before us in the
  2840. * exec_object list, so it should have a GTT space bound by now.
  2841. */
  2842. if (target_obj_priv->gtt_space == NULL) {
  2843. DRM_ERROR("No GTT space found for object %d\n",
  2844. reloc->target_handle);
  2845. drm_gem_object_unreference(target_obj);
  2846. i915_gem_object_unpin(obj);
  2847. return -EINVAL;
  2848. }
  2849. /* Validate that the target is in a valid r/w GPU domain */
  2850. if (reloc->write_domain & (reloc->write_domain - 1)) {
  2851. DRM_ERROR("reloc with multiple write domains: "
  2852. "obj %p target %d offset %d "
  2853. "read %08x write %08x",
  2854. obj, reloc->target_handle,
  2855. (int) reloc->offset,
  2856. reloc->read_domains,
  2857. reloc->write_domain);
  2858. return -EINVAL;
  2859. }
  2860. if (reloc->write_domain & I915_GEM_DOMAIN_CPU ||
  2861. reloc->read_domains & I915_GEM_DOMAIN_CPU) {
  2862. DRM_ERROR("reloc with read/write CPU domains: "
  2863. "obj %p target %d offset %d "
  2864. "read %08x write %08x",
  2865. obj, reloc->target_handle,
  2866. (int) reloc->offset,
  2867. reloc->read_domains,
  2868. reloc->write_domain);
  2869. drm_gem_object_unreference(target_obj);
  2870. i915_gem_object_unpin(obj);
  2871. return -EINVAL;
  2872. }
  2873. if (reloc->write_domain && target_obj->pending_write_domain &&
  2874. reloc->write_domain != target_obj->pending_write_domain) {
  2875. DRM_ERROR("Write domain conflict: "
  2876. "obj %p target %d offset %d "
  2877. "new %08x old %08x\n",
  2878. obj, reloc->target_handle,
  2879. (int) reloc->offset,
  2880. reloc->write_domain,
  2881. target_obj->pending_write_domain);
  2882. drm_gem_object_unreference(target_obj);
  2883. i915_gem_object_unpin(obj);
  2884. return -EINVAL;
  2885. }
  2886. target_obj->pending_read_domains |= reloc->read_domains;
  2887. target_obj->pending_write_domain |= reloc->write_domain;
  2888. /* If the relocation already has the right value in it, no
  2889. * more work needs to be done.
  2890. */
  2891. if (target_obj_priv->gtt_offset == reloc->presumed_offset) {
  2892. drm_gem_object_unreference(target_obj);
  2893. continue;
  2894. }
  2895. /* Check that the relocation address is valid... */
  2896. if (reloc->offset > obj->size - 4) {
  2897. DRM_ERROR("Relocation beyond object bounds: "
  2898. "obj %p target %d offset %d size %d.\n",
  2899. obj, reloc->target_handle,
  2900. (int) reloc->offset, (int) obj->size);
  2901. drm_gem_object_unreference(target_obj);
  2902. i915_gem_object_unpin(obj);
  2903. return -EINVAL;
  2904. }
  2905. if (reloc->offset & 3) {
  2906. DRM_ERROR("Relocation not 4-byte aligned: "
  2907. "obj %p target %d offset %d.\n",
  2908. obj, reloc->target_handle,
  2909. (int) reloc->offset);
  2910. drm_gem_object_unreference(target_obj);
  2911. i915_gem_object_unpin(obj);
  2912. return -EINVAL;
  2913. }
  2914. /* and points to somewhere within the target object. */
  2915. if (reloc->delta >= target_obj->size) {
  2916. DRM_ERROR("Relocation beyond target object bounds: "
  2917. "obj %p target %d delta %d size %d.\n",
  2918. obj, reloc->target_handle,
  2919. (int) reloc->delta, (int) target_obj->size);
  2920. drm_gem_object_unreference(target_obj);
  2921. i915_gem_object_unpin(obj);
  2922. return -EINVAL;
  2923. }
  2924. ret = i915_gem_object_set_to_gtt_domain(obj, 1);
  2925. if (ret != 0) {
  2926. drm_gem_object_unreference(target_obj);
  2927. i915_gem_object_unpin(obj);
  2928. return -EINVAL;
  2929. }
  2930. /* Map the page containing the relocation we're going to
  2931. * perform.
  2932. */
  2933. reloc_offset = obj_priv->gtt_offset + reloc->offset;
  2934. reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
  2935. (reloc_offset &
  2936. ~(PAGE_SIZE - 1)));
  2937. reloc_entry = (uint32_t __iomem *)(reloc_page +
  2938. (reloc_offset & (PAGE_SIZE - 1)));
  2939. reloc_val = target_obj_priv->gtt_offset + reloc->delta;
  2940. #if WATCH_BUF
  2941. DRM_INFO("Applied relocation: %p@0x%08x %08x -> %08x\n",
  2942. obj, (unsigned int) reloc->offset,
  2943. readl(reloc_entry), reloc_val);
  2944. #endif
  2945. writel(reloc_val, reloc_entry);
  2946. io_mapping_unmap_atomic(reloc_page);
  2947. /* The updated presumed offset for this entry will be
  2948. * copied back out to the user.
  2949. */
  2950. reloc->presumed_offset = target_obj_priv->gtt_offset;
  2951. drm_gem_object_unreference(target_obj);
  2952. }
  2953. #if WATCH_BUF
  2954. if (0)
  2955. i915_gem_dump_object(obj, 128, __func__, ~0);
  2956. #endif
  2957. return 0;
  2958. }
  2959. /* Throttle our rendering by waiting until the ring has completed our requests
  2960. * emitted over 20 msec ago.
  2961. *
  2962. * Note that if we were to use the current jiffies each time around the loop,
  2963. * we wouldn't escape the function with any frames outstanding if the time to
  2964. * render a frame was over 20ms.
  2965. *
  2966. * This should get us reasonable parallelism between CPU and GPU but also
  2967. * relatively low latency when blocking on a particular request to finish.
  2968. */
  2969. static int
  2970. i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file_priv)
  2971. {
  2972. struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
  2973. int ret = 0;
  2974. unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
  2975. mutex_lock(&dev->struct_mutex);
  2976. while (!list_empty(&i915_file_priv->mm.request_list)) {
  2977. struct drm_i915_gem_request *request;
  2978. request = list_first_entry(&i915_file_priv->mm.request_list,
  2979. struct drm_i915_gem_request,
  2980. client_list);
  2981. if (time_after_eq(request->emitted_jiffies, recent_enough))
  2982. break;
  2983. ret = i915_wait_request(dev, request->seqno, request->ring);
  2984. if (ret != 0)
  2985. break;
  2986. }
  2987. mutex_unlock(&dev->struct_mutex);
  2988. return ret;
  2989. }
  2990. static int
  2991. i915_gem_get_relocs_from_user(struct drm_i915_gem_exec_object2 *exec_list,
  2992. uint32_t buffer_count,
  2993. struct drm_i915_gem_relocation_entry **relocs)
  2994. {
  2995. uint32_t reloc_count = 0, reloc_index = 0, i;
  2996. int ret;
  2997. *relocs = NULL;
  2998. for (i = 0; i < buffer_count; i++) {
  2999. if (reloc_count + exec_list[i].relocation_count < reloc_count)
  3000. return -EINVAL;
  3001. reloc_count += exec_list[i].relocation_count;
  3002. }
  3003. *relocs = drm_calloc_large(reloc_count, sizeof(**relocs));
  3004. if (*relocs == NULL) {
  3005. DRM_ERROR("failed to alloc relocs, count %d\n", reloc_count);
  3006. return -ENOMEM;
  3007. }
  3008. for (i = 0; i < buffer_count; i++) {
  3009. struct drm_i915_gem_relocation_entry __user *user_relocs;
  3010. user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
  3011. ret = copy_from_user(&(*relocs)[reloc_index],
  3012. user_relocs,
  3013. exec_list[i].relocation_count *
  3014. sizeof(**relocs));
  3015. if (ret != 0) {
  3016. drm_free_large(*relocs);
  3017. *relocs = NULL;
  3018. return -EFAULT;
  3019. }
  3020. reloc_index += exec_list[i].relocation_count;
  3021. }
  3022. return 0;
  3023. }
  3024. static int
  3025. i915_gem_put_relocs_to_user(struct drm_i915_gem_exec_object2 *exec_list,
  3026. uint32_t buffer_count,
  3027. struct drm_i915_gem_relocation_entry *relocs)
  3028. {
  3029. uint32_t reloc_count = 0, i;
  3030. int ret = 0;
  3031. if (relocs == NULL)
  3032. return 0;
  3033. for (i = 0; i < buffer_count; i++) {
  3034. struct drm_i915_gem_relocation_entry __user *user_relocs;
  3035. int unwritten;
  3036. user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
  3037. unwritten = copy_to_user(user_relocs,
  3038. &relocs[reloc_count],
  3039. exec_list[i].relocation_count *
  3040. sizeof(*relocs));
  3041. if (unwritten) {
  3042. ret = -EFAULT;
  3043. goto err;
  3044. }
  3045. reloc_count += exec_list[i].relocation_count;
  3046. }
  3047. err:
  3048. drm_free_large(relocs);
  3049. return ret;
  3050. }
  3051. static int
  3052. i915_gem_check_execbuffer (struct drm_i915_gem_execbuffer2 *exec,
  3053. uint64_t exec_offset)
  3054. {
  3055. uint32_t exec_start, exec_len;
  3056. exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
  3057. exec_len = (uint32_t) exec->batch_len;
  3058. if ((exec_start | exec_len) & 0x7)
  3059. return -EINVAL;
  3060. if (!exec_start)
  3061. return -EINVAL;
  3062. return 0;
  3063. }
  3064. static int
  3065. i915_gem_wait_for_pending_flip(struct drm_device *dev,
  3066. struct drm_gem_object **object_list,
  3067. int count)
  3068. {
  3069. drm_i915_private_t *dev_priv = dev->dev_private;
  3070. struct drm_i915_gem_object *obj_priv;
  3071. DEFINE_WAIT(wait);
  3072. int i, ret = 0;
  3073. for (;;) {
  3074. prepare_to_wait(&dev_priv->pending_flip_queue,
  3075. &wait, TASK_INTERRUPTIBLE);
  3076. for (i = 0; i < count; i++) {
  3077. obj_priv = to_intel_bo(object_list[i]);
  3078. if (atomic_read(&obj_priv->pending_flip) > 0)
  3079. break;
  3080. }
  3081. if (i == count)
  3082. break;
  3083. if (!signal_pending(current)) {
  3084. mutex_unlock(&dev->struct_mutex);
  3085. schedule();
  3086. mutex_lock(&dev->struct_mutex);
  3087. continue;
  3088. }
  3089. ret = -ERESTARTSYS;
  3090. break;
  3091. }
  3092. finish_wait(&dev_priv->pending_flip_queue, &wait);
  3093. return ret;
  3094. }
  3095. int
  3096. i915_gem_do_execbuffer(struct drm_device *dev, void *data,
  3097. struct drm_file *file_priv,
  3098. struct drm_i915_gem_execbuffer2 *args,
  3099. struct drm_i915_gem_exec_object2 *exec_list)
  3100. {
  3101. drm_i915_private_t *dev_priv = dev->dev_private;
  3102. struct drm_gem_object **object_list = NULL;
  3103. struct drm_gem_object *batch_obj;
  3104. struct drm_i915_gem_object *obj_priv;
  3105. struct drm_clip_rect *cliprects = NULL;
  3106. struct drm_i915_gem_relocation_entry *relocs = NULL;
  3107. int ret = 0, ret2, i, pinned = 0;
  3108. uint64_t exec_offset;
  3109. uint32_t seqno, flush_domains, reloc_index;
  3110. int pin_tries, flips;
  3111. struct intel_ring_buffer *ring = NULL;
  3112. #if WATCH_EXEC
  3113. DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
  3114. (int) args->buffers_ptr, args->buffer_count, args->batch_len);
  3115. #endif
  3116. if (args->flags & I915_EXEC_BSD) {
  3117. if (!HAS_BSD(dev)) {
  3118. DRM_ERROR("execbuf with wrong flag\n");
  3119. return -EINVAL;
  3120. }
  3121. ring = &dev_priv->bsd_ring;
  3122. } else {
  3123. ring = &dev_priv->render_ring;
  3124. }
  3125. if (args->buffer_count < 1) {
  3126. DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
  3127. return -EINVAL;
  3128. }
  3129. object_list = drm_malloc_ab(sizeof(*object_list), args->buffer_count);
  3130. if (object_list == NULL) {
  3131. DRM_ERROR("Failed to allocate object list for %d buffers\n",
  3132. args->buffer_count);
  3133. ret = -ENOMEM;
  3134. goto pre_mutex_err;
  3135. }
  3136. if (args->num_cliprects != 0) {
  3137. cliprects = kcalloc(args->num_cliprects, sizeof(*cliprects),
  3138. GFP_KERNEL);
  3139. if (cliprects == NULL) {
  3140. ret = -ENOMEM;
  3141. goto pre_mutex_err;
  3142. }
  3143. ret = copy_from_user(cliprects,
  3144. (struct drm_clip_rect __user *)
  3145. (uintptr_t) args->cliprects_ptr,
  3146. sizeof(*cliprects) * args->num_cliprects);
  3147. if (ret != 0) {
  3148. DRM_ERROR("copy %d cliprects failed: %d\n",
  3149. args->num_cliprects, ret);
  3150. goto pre_mutex_err;
  3151. }
  3152. }
  3153. ret = i915_gem_get_relocs_from_user(exec_list, args->buffer_count,
  3154. &relocs);
  3155. if (ret != 0)
  3156. goto pre_mutex_err;
  3157. mutex_lock(&dev->struct_mutex);
  3158. i915_verify_inactive(dev, __FILE__, __LINE__);
  3159. if (atomic_read(&dev_priv->mm.wedged)) {
  3160. mutex_unlock(&dev->struct_mutex);
  3161. ret = -EIO;
  3162. goto pre_mutex_err;
  3163. }
  3164. if (dev_priv->mm.suspended) {
  3165. mutex_unlock(&dev->struct_mutex);
  3166. ret = -EBUSY;
  3167. goto pre_mutex_err;
  3168. }
  3169. /* Look up object handles */
  3170. flips = 0;
  3171. for (i = 0; i < args->buffer_count; i++) {
  3172. object_list[i] = drm_gem_object_lookup(dev, file_priv,
  3173. exec_list[i].handle);
  3174. if (object_list[i] == NULL) {
  3175. DRM_ERROR("Invalid object handle %d at index %d\n",
  3176. exec_list[i].handle, i);
  3177. /* prevent error path from reading uninitialized data */
  3178. args->buffer_count = i + 1;
  3179. ret = -EBADF;
  3180. goto err;
  3181. }
  3182. obj_priv = to_intel_bo(object_list[i]);
  3183. if (obj_priv->in_execbuffer) {
  3184. DRM_ERROR("Object %p appears more than once in object list\n",
  3185. object_list[i]);
  3186. /* prevent error path from reading uninitialized data */
  3187. args->buffer_count = i + 1;
  3188. ret = -EBADF;
  3189. goto err;
  3190. }
  3191. obj_priv->in_execbuffer = true;
  3192. flips += atomic_read(&obj_priv->pending_flip);
  3193. }
  3194. if (flips > 0) {
  3195. ret = i915_gem_wait_for_pending_flip(dev, object_list,
  3196. args->buffer_count);
  3197. if (ret)
  3198. goto err;
  3199. }
  3200. /* Pin and relocate */
  3201. for (pin_tries = 0; ; pin_tries++) {
  3202. ret = 0;
  3203. reloc_index = 0;
  3204. for (i = 0; i < args->buffer_count; i++) {
  3205. object_list[i]->pending_read_domains = 0;
  3206. object_list[i]->pending_write_domain = 0;
  3207. ret = i915_gem_object_pin_and_relocate(object_list[i],
  3208. file_priv,
  3209. &exec_list[i],
  3210. &relocs[reloc_index]);
  3211. if (ret)
  3212. break;
  3213. pinned = i + 1;
  3214. reloc_index += exec_list[i].relocation_count;
  3215. }
  3216. /* success */
  3217. if (ret == 0)
  3218. break;
  3219. /* error other than GTT full, or we've already tried again */
  3220. if (ret != -ENOSPC || pin_tries >= 1) {
  3221. if (ret != -ERESTARTSYS) {
  3222. unsigned long long total_size = 0;
  3223. int num_fences = 0;
  3224. for (i = 0; i < args->buffer_count; i++) {
  3225. obj_priv = to_intel_bo(object_list[i]);
  3226. total_size += object_list[i]->size;
  3227. num_fences +=
  3228. exec_list[i].flags & EXEC_OBJECT_NEEDS_FENCE &&
  3229. obj_priv->tiling_mode != I915_TILING_NONE;
  3230. }
  3231. DRM_ERROR("Failed to pin buffer %d of %d, total %llu bytes, %d fences: %d\n",
  3232. pinned+1, args->buffer_count,
  3233. total_size, num_fences,
  3234. ret);
  3235. DRM_ERROR("%d objects [%d pinned], "
  3236. "%d object bytes [%d pinned], "
  3237. "%d/%d gtt bytes\n",
  3238. atomic_read(&dev->object_count),
  3239. atomic_read(&dev->pin_count),
  3240. atomic_read(&dev->object_memory),
  3241. atomic_read(&dev->pin_memory),
  3242. atomic_read(&dev->gtt_memory),
  3243. dev->gtt_total);
  3244. }
  3245. goto err;
  3246. }
  3247. /* unpin all of our buffers */
  3248. for (i = 0; i < pinned; i++)
  3249. i915_gem_object_unpin(object_list[i]);
  3250. pinned = 0;
  3251. /* evict everyone we can from the aperture */
  3252. ret = i915_gem_evict_everything(dev);
  3253. if (ret && ret != -ENOSPC)
  3254. goto err;
  3255. }
  3256. /* Set the pending read domains for the batch buffer to COMMAND */
  3257. batch_obj = object_list[args->buffer_count-1];
  3258. if (batch_obj->pending_write_domain) {
  3259. DRM_ERROR("Attempting to use self-modifying batch buffer\n");
  3260. ret = -EINVAL;
  3261. goto err;
  3262. }
  3263. batch_obj->pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
  3264. /* Sanity check the batch buffer, prior to moving objects */
  3265. exec_offset = exec_list[args->buffer_count - 1].offset;
  3266. ret = i915_gem_check_execbuffer (args, exec_offset);
  3267. if (ret != 0) {
  3268. DRM_ERROR("execbuf with invalid offset/length\n");
  3269. goto err;
  3270. }
  3271. i915_verify_inactive(dev, __FILE__, __LINE__);
  3272. /* Zero the global flush/invalidate flags. These
  3273. * will be modified as new domains are computed
  3274. * for each object
  3275. */
  3276. dev->invalidate_domains = 0;
  3277. dev->flush_domains = 0;
  3278. for (i = 0; i < args->buffer_count; i++) {
  3279. struct drm_gem_object *obj = object_list[i];
  3280. /* Compute new gpu domains and update invalidate/flush */
  3281. i915_gem_object_set_to_gpu_domain(obj);
  3282. }
  3283. i915_verify_inactive(dev, __FILE__, __LINE__);
  3284. if (dev->invalidate_domains | dev->flush_domains) {
  3285. #if WATCH_EXEC
  3286. DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n",
  3287. __func__,
  3288. dev->invalidate_domains,
  3289. dev->flush_domains);
  3290. #endif
  3291. i915_gem_flush(dev,
  3292. dev->invalidate_domains,
  3293. dev->flush_domains);
  3294. if (dev->flush_domains & I915_GEM_GPU_DOMAINS) {
  3295. (void)i915_add_request(dev, file_priv,
  3296. dev->flush_domains,
  3297. &dev_priv->render_ring);
  3298. if (HAS_BSD(dev))
  3299. (void)i915_add_request(dev, file_priv,
  3300. dev->flush_domains,
  3301. &dev_priv->bsd_ring);
  3302. }
  3303. }
  3304. for (i = 0; i < args->buffer_count; i++) {
  3305. struct drm_gem_object *obj = object_list[i];
  3306. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  3307. uint32_t old_write_domain = obj->write_domain;
  3308. obj->write_domain = obj->pending_write_domain;
  3309. if (obj->write_domain)
  3310. list_move_tail(&obj_priv->gpu_write_list,
  3311. &dev_priv->mm.gpu_write_list);
  3312. else
  3313. list_del_init(&obj_priv->gpu_write_list);
  3314. trace_i915_gem_object_change_domain(obj,
  3315. obj->read_domains,
  3316. old_write_domain);
  3317. }
  3318. i915_verify_inactive(dev, __FILE__, __LINE__);
  3319. #if WATCH_COHERENCY
  3320. for (i = 0; i < args->buffer_count; i++) {
  3321. i915_gem_object_check_coherency(object_list[i],
  3322. exec_list[i].handle);
  3323. }
  3324. #endif
  3325. #if WATCH_EXEC
  3326. i915_gem_dump_object(batch_obj,
  3327. args->batch_len,
  3328. __func__,
  3329. ~0);
  3330. #endif
  3331. /* Exec the batchbuffer */
  3332. ret = ring->dispatch_gem_execbuffer(dev, ring, args,
  3333. cliprects, exec_offset);
  3334. if (ret) {
  3335. DRM_ERROR("dispatch failed %d\n", ret);
  3336. goto err;
  3337. }
  3338. /*
  3339. * Ensure that the commands in the batch buffer are
  3340. * finished before the interrupt fires
  3341. */
  3342. flush_domains = i915_retire_commands(dev, ring);
  3343. i915_verify_inactive(dev, __FILE__, __LINE__);
  3344. /*
  3345. * Get a seqno representing the execution of the current buffer,
  3346. * which we can wait on. We would like to mitigate these interrupts,
  3347. * likely by only creating seqnos occasionally (so that we have
  3348. * *some* interrupts representing completion of buffers that we can
  3349. * wait on when trying to clear up gtt space).
  3350. */
  3351. seqno = i915_add_request(dev, file_priv, flush_domains, ring);
  3352. BUG_ON(seqno == 0);
  3353. for (i = 0; i < args->buffer_count; i++) {
  3354. struct drm_gem_object *obj = object_list[i];
  3355. obj_priv = to_intel_bo(obj);
  3356. i915_gem_object_move_to_active(obj, seqno, ring);
  3357. #if WATCH_LRU
  3358. DRM_INFO("%s: move to exec list %p\n", __func__, obj);
  3359. #endif
  3360. }
  3361. #if WATCH_LRU
  3362. i915_dump_lru(dev, __func__);
  3363. #endif
  3364. i915_verify_inactive(dev, __FILE__, __LINE__);
  3365. err:
  3366. for (i = 0; i < pinned; i++)
  3367. i915_gem_object_unpin(object_list[i]);
  3368. for (i = 0; i < args->buffer_count; i++) {
  3369. if (object_list[i]) {
  3370. obj_priv = to_intel_bo(object_list[i]);
  3371. obj_priv->in_execbuffer = false;
  3372. }
  3373. drm_gem_object_unreference(object_list[i]);
  3374. }
  3375. mutex_unlock(&dev->struct_mutex);
  3376. pre_mutex_err:
  3377. /* Copy the updated relocations out regardless of current error
  3378. * state. Failure to update the relocs would mean that the next
  3379. * time userland calls execbuf, it would do so with presumed offset
  3380. * state that didn't match the actual object state.
  3381. */
  3382. ret2 = i915_gem_put_relocs_to_user(exec_list, args->buffer_count,
  3383. relocs);
  3384. if (ret2 != 0) {
  3385. DRM_ERROR("Failed to copy relocations back out: %d\n", ret2);
  3386. if (ret == 0)
  3387. ret = ret2;
  3388. }
  3389. drm_free_large(object_list);
  3390. kfree(cliprects);
  3391. return ret;
  3392. }
  3393. /*
  3394. * Legacy execbuffer just creates an exec2 list from the original exec object
  3395. * list array and passes it to the real function.
  3396. */
  3397. int
  3398. i915_gem_execbuffer(struct drm_device *dev, void *data,
  3399. struct drm_file *file_priv)
  3400. {
  3401. struct drm_i915_gem_execbuffer *args = data;
  3402. struct drm_i915_gem_execbuffer2 exec2;
  3403. struct drm_i915_gem_exec_object *exec_list = NULL;
  3404. struct drm_i915_gem_exec_object2 *exec2_list = NULL;
  3405. int ret, i;
  3406. #if WATCH_EXEC
  3407. DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
  3408. (int) args->buffers_ptr, args->buffer_count, args->batch_len);
  3409. #endif
  3410. if (args->buffer_count < 1) {
  3411. DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
  3412. return -EINVAL;
  3413. }
  3414. /* Copy in the exec list from userland */
  3415. exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
  3416. exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
  3417. if (exec_list == NULL || exec2_list == NULL) {
  3418. DRM_ERROR("Failed to allocate exec list for %d buffers\n",
  3419. args->buffer_count);
  3420. drm_free_large(exec_list);
  3421. drm_free_large(exec2_list);
  3422. return -ENOMEM;
  3423. }
  3424. ret = copy_from_user(exec_list,
  3425. (struct drm_i915_relocation_entry __user *)
  3426. (uintptr_t) args->buffers_ptr,
  3427. sizeof(*exec_list) * args->buffer_count);
  3428. if (ret != 0) {
  3429. DRM_ERROR("copy %d exec entries failed %d\n",
  3430. args->buffer_count, ret);
  3431. drm_free_large(exec_list);
  3432. drm_free_large(exec2_list);
  3433. return -EFAULT;
  3434. }
  3435. for (i = 0; i < args->buffer_count; i++) {
  3436. exec2_list[i].handle = exec_list[i].handle;
  3437. exec2_list[i].relocation_count = exec_list[i].relocation_count;
  3438. exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
  3439. exec2_list[i].alignment = exec_list[i].alignment;
  3440. exec2_list[i].offset = exec_list[i].offset;
  3441. if (!IS_I965G(dev))
  3442. exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
  3443. else
  3444. exec2_list[i].flags = 0;
  3445. }
  3446. exec2.buffers_ptr = args->buffers_ptr;
  3447. exec2.buffer_count = args->buffer_count;
  3448. exec2.batch_start_offset = args->batch_start_offset;
  3449. exec2.batch_len = args->batch_len;
  3450. exec2.DR1 = args->DR1;
  3451. exec2.DR4 = args->DR4;
  3452. exec2.num_cliprects = args->num_cliprects;
  3453. exec2.cliprects_ptr = args->cliprects_ptr;
  3454. exec2.flags = I915_EXEC_RENDER;
  3455. ret = i915_gem_do_execbuffer(dev, data, file_priv, &exec2, exec2_list);
  3456. if (!ret) {
  3457. /* Copy the new buffer offsets back to the user's exec list. */
  3458. for (i = 0; i < args->buffer_count; i++)
  3459. exec_list[i].offset = exec2_list[i].offset;
  3460. /* ... and back out to userspace */
  3461. ret = copy_to_user((struct drm_i915_relocation_entry __user *)
  3462. (uintptr_t) args->buffers_ptr,
  3463. exec_list,
  3464. sizeof(*exec_list) * args->buffer_count);
  3465. if (ret) {
  3466. ret = -EFAULT;
  3467. DRM_ERROR("failed to copy %d exec entries "
  3468. "back to user (%d)\n",
  3469. args->buffer_count, ret);
  3470. }
  3471. }
  3472. drm_free_large(exec_list);
  3473. drm_free_large(exec2_list);
  3474. return ret;
  3475. }
  3476. int
  3477. i915_gem_execbuffer2(struct drm_device *dev, void *data,
  3478. struct drm_file *file_priv)
  3479. {
  3480. struct drm_i915_gem_execbuffer2 *args = data;
  3481. struct drm_i915_gem_exec_object2 *exec2_list = NULL;
  3482. int ret;
  3483. #if WATCH_EXEC
  3484. DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
  3485. (int) args->buffers_ptr, args->buffer_count, args->batch_len);
  3486. #endif
  3487. if (args->buffer_count < 1) {
  3488. DRM_ERROR("execbuf2 with %d buffers\n", args->buffer_count);
  3489. return -EINVAL;
  3490. }
  3491. exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
  3492. if (exec2_list == NULL) {
  3493. DRM_ERROR("Failed to allocate exec list for %d buffers\n",
  3494. args->buffer_count);
  3495. return -ENOMEM;
  3496. }
  3497. ret = copy_from_user(exec2_list,
  3498. (struct drm_i915_relocation_entry __user *)
  3499. (uintptr_t) args->buffers_ptr,
  3500. sizeof(*exec2_list) * args->buffer_count);
  3501. if (ret != 0) {
  3502. DRM_ERROR("copy %d exec entries failed %d\n",
  3503. args->buffer_count, ret);
  3504. drm_free_large(exec2_list);
  3505. return -EFAULT;
  3506. }
  3507. ret = i915_gem_do_execbuffer(dev, data, file_priv, args, exec2_list);
  3508. if (!ret) {
  3509. /* Copy the new buffer offsets back to the user's exec list. */
  3510. ret = copy_to_user((struct drm_i915_relocation_entry __user *)
  3511. (uintptr_t) args->buffers_ptr,
  3512. exec2_list,
  3513. sizeof(*exec2_list) * args->buffer_count);
  3514. if (ret) {
  3515. ret = -EFAULT;
  3516. DRM_ERROR("failed to copy %d exec entries "
  3517. "back to user (%d)\n",
  3518. args->buffer_count, ret);
  3519. }
  3520. }
  3521. drm_free_large(exec2_list);
  3522. return ret;
  3523. }
  3524. int
  3525. i915_gem_object_pin(struct drm_gem_object *obj, uint32_t alignment)
  3526. {
  3527. struct drm_device *dev = obj->dev;
  3528. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  3529. int ret;
  3530. BUG_ON(obj_priv->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT);
  3531. i915_verify_inactive(dev, __FILE__, __LINE__);
  3532. if (obj_priv->gtt_space != NULL) {
  3533. if (alignment == 0)
  3534. alignment = i915_gem_get_gtt_alignment(obj);
  3535. if (obj_priv->gtt_offset & (alignment - 1)) {
  3536. ret = i915_gem_object_unbind(obj);
  3537. if (ret)
  3538. return ret;
  3539. }
  3540. }
  3541. if (obj_priv->gtt_space == NULL) {
  3542. ret = i915_gem_object_bind_to_gtt(obj, alignment);
  3543. if (ret)
  3544. return ret;
  3545. }
  3546. obj_priv->pin_count++;
  3547. /* If the object is not active and not pending a flush,
  3548. * remove it from the inactive list
  3549. */
  3550. if (obj_priv->pin_count == 1) {
  3551. atomic_inc(&dev->pin_count);
  3552. atomic_add(obj->size, &dev->pin_memory);
  3553. if (!obj_priv->active &&
  3554. (obj->write_domain & I915_GEM_GPU_DOMAINS) == 0 &&
  3555. !list_empty(&obj_priv->list))
  3556. list_del_init(&obj_priv->list);
  3557. }
  3558. i915_verify_inactive(dev, __FILE__, __LINE__);
  3559. return 0;
  3560. }
  3561. void
  3562. i915_gem_object_unpin(struct drm_gem_object *obj)
  3563. {
  3564. struct drm_device *dev = obj->dev;
  3565. drm_i915_private_t *dev_priv = dev->dev_private;
  3566. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  3567. i915_verify_inactive(dev, __FILE__, __LINE__);
  3568. obj_priv->pin_count--;
  3569. BUG_ON(obj_priv->pin_count < 0);
  3570. BUG_ON(obj_priv->gtt_space == NULL);
  3571. /* If the object is no longer pinned, and is
  3572. * neither active nor being flushed, then stick it on
  3573. * the inactive list
  3574. */
  3575. if (obj_priv->pin_count == 0) {
  3576. if (!obj_priv->active &&
  3577. (obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
  3578. list_move_tail(&obj_priv->list,
  3579. &dev_priv->mm.inactive_list);
  3580. atomic_dec(&dev->pin_count);
  3581. atomic_sub(obj->size, &dev->pin_memory);
  3582. }
  3583. i915_verify_inactive(dev, __FILE__, __LINE__);
  3584. }
  3585. int
  3586. i915_gem_pin_ioctl(struct drm_device *dev, void *data,
  3587. struct drm_file *file_priv)
  3588. {
  3589. struct drm_i915_gem_pin *args = data;
  3590. struct drm_gem_object *obj;
  3591. struct drm_i915_gem_object *obj_priv;
  3592. int ret;
  3593. mutex_lock(&dev->struct_mutex);
  3594. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  3595. if (obj == NULL) {
  3596. DRM_ERROR("Bad handle in i915_gem_pin_ioctl(): %d\n",
  3597. args->handle);
  3598. mutex_unlock(&dev->struct_mutex);
  3599. return -EBADF;
  3600. }
  3601. obj_priv = to_intel_bo(obj);
  3602. if (obj_priv->madv != I915_MADV_WILLNEED) {
  3603. DRM_ERROR("Attempting to pin a purgeable buffer\n");
  3604. drm_gem_object_unreference(obj);
  3605. mutex_unlock(&dev->struct_mutex);
  3606. return -EINVAL;
  3607. }
  3608. if (obj_priv->pin_filp != NULL && obj_priv->pin_filp != file_priv) {
  3609. DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
  3610. args->handle);
  3611. drm_gem_object_unreference(obj);
  3612. mutex_unlock(&dev->struct_mutex);
  3613. return -EINVAL;
  3614. }
  3615. obj_priv->user_pin_count++;
  3616. obj_priv->pin_filp = file_priv;
  3617. if (obj_priv->user_pin_count == 1) {
  3618. ret = i915_gem_object_pin(obj, args->alignment);
  3619. if (ret != 0) {
  3620. drm_gem_object_unreference(obj);
  3621. mutex_unlock(&dev->struct_mutex);
  3622. return ret;
  3623. }
  3624. }
  3625. /* XXX - flush the CPU caches for pinned objects
  3626. * as the X server doesn't manage domains yet
  3627. */
  3628. i915_gem_object_flush_cpu_write_domain(obj);
  3629. args->offset = obj_priv->gtt_offset;
  3630. drm_gem_object_unreference(obj);
  3631. mutex_unlock(&dev->struct_mutex);
  3632. return 0;
  3633. }
  3634. int
  3635. i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
  3636. struct drm_file *file_priv)
  3637. {
  3638. struct drm_i915_gem_pin *args = data;
  3639. struct drm_gem_object *obj;
  3640. struct drm_i915_gem_object *obj_priv;
  3641. mutex_lock(&dev->struct_mutex);
  3642. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  3643. if (obj == NULL) {
  3644. DRM_ERROR("Bad handle in i915_gem_unpin_ioctl(): %d\n",
  3645. args->handle);
  3646. mutex_unlock(&dev->struct_mutex);
  3647. return -EBADF;
  3648. }
  3649. obj_priv = to_intel_bo(obj);
  3650. if (obj_priv->pin_filp != file_priv) {
  3651. DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
  3652. args->handle);
  3653. drm_gem_object_unreference(obj);
  3654. mutex_unlock(&dev->struct_mutex);
  3655. return -EINVAL;
  3656. }
  3657. obj_priv->user_pin_count--;
  3658. if (obj_priv->user_pin_count == 0) {
  3659. obj_priv->pin_filp = NULL;
  3660. i915_gem_object_unpin(obj);
  3661. }
  3662. drm_gem_object_unreference(obj);
  3663. mutex_unlock(&dev->struct_mutex);
  3664. return 0;
  3665. }
  3666. int
  3667. i915_gem_busy_ioctl(struct drm_device *dev, void *data,
  3668. struct drm_file *file_priv)
  3669. {
  3670. struct drm_i915_gem_busy *args = data;
  3671. struct drm_gem_object *obj;
  3672. struct drm_i915_gem_object *obj_priv;
  3673. drm_i915_private_t *dev_priv = dev->dev_private;
  3674. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  3675. if (obj == NULL) {
  3676. DRM_ERROR("Bad handle in i915_gem_busy_ioctl(): %d\n",
  3677. args->handle);
  3678. return -EBADF;
  3679. }
  3680. mutex_lock(&dev->struct_mutex);
  3681. /* Update the active list for the hardware's current position.
  3682. * Otherwise this only updates on a delayed timer or when irqs are
  3683. * actually unmasked, and our working set ends up being larger than
  3684. * required.
  3685. */
  3686. i915_gem_retire_requests(dev, &dev_priv->render_ring);
  3687. if (HAS_BSD(dev))
  3688. i915_gem_retire_requests(dev, &dev_priv->bsd_ring);
  3689. obj_priv = to_intel_bo(obj);
  3690. /* Don't count being on the flushing list against the object being
  3691. * done. Otherwise, a buffer left on the flushing list but not getting
  3692. * flushed (because nobody's flushing that domain) won't ever return
  3693. * unbusy and get reused by libdrm's bo cache. The other expected
  3694. * consumer of this interface, OpenGL's occlusion queries, also specs
  3695. * that the objects get unbusy "eventually" without any interference.
  3696. */
  3697. args->busy = obj_priv->active && obj_priv->last_rendering_seqno != 0;
  3698. drm_gem_object_unreference(obj);
  3699. mutex_unlock(&dev->struct_mutex);
  3700. return 0;
  3701. }
  3702. int
  3703. i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
  3704. struct drm_file *file_priv)
  3705. {
  3706. return i915_gem_ring_throttle(dev, file_priv);
  3707. }
  3708. int
  3709. i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
  3710. struct drm_file *file_priv)
  3711. {
  3712. struct drm_i915_gem_madvise *args = data;
  3713. struct drm_gem_object *obj;
  3714. struct drm_i915_gem_object *obj_priv;
  3715. switch (args->madv) {
  3716. case I915_MADV_DONTNEED:
  3717. case I915_MADV_WILLNEED:
  3718. break;
  3719. default:
  3720. return -EINVAL;
  3721. }
  3722. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  3723. if (obj == NULL) {
  3724. DRM_ERROR("Bad handle in i915_gem_madvise_ioctl(): %d\n",
  3725. args->handle);
  3726. return -EBADF;
  3727. }
  3728. mutex_lock(&dev->struct_mutex);
  3729. obj_priv = to_intel_bo(obj);
  3730. if (obj_priv->pin_count) {
  3731. drm_gem_object_unreference(obj);
  3732. mutex_unlock(&dev->struct_mutex);
  3733. DRM_ERROR("Attempted i915_gem_madvise_ioctl() on a pinned object\n");
  3734. return -EINVAL;
  3735. }
  3736. if (obj_priv->madv != __I915_MADV_PURGED)
  3737. obj_priv->madv = args->madv;
  3738. /* if the object is no longer bound, discard its backing storage */
  3739. if (i915_gem_object_is_purgeable(obj_priv) &&
  3740. obj_priv->gtt_space == NULL)
  3741. i915_gem_object_truncate(obj);
  3742. args->retained = obj_priv->madv != __I915_MADV_PURGED;
  3743. drm_gem_object_unreference(obj);
  3744. mutex_unlock(&dev->struct_mutex);
  3745. return 0;
  3746. }
  3747. struct drm_gem_object * i915_gem_alloc_object(struct drm_device *dev,
  3748. size_t size)
  3749. {
  3750. struct drm_i915_gem_object *obj;
  3751. obj = kzalloc(sizeof(*obj), GFP_KERNEL);
  3752. if (obj == NULL)
  3753. return NULL;
  3754. if (drm_gem_object_init(dev, &obj->base, size) != 0) {
  3755. kfree(obj);
  3756. return NULL;
  3757. }
  3758. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  3759. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  3760. obj->agp_type = AGP_USER_MEMORY;
  3761. obj->base.driver_private = NULL;
  3762. obj->fence_reg = I915_FENCE_REG_NONE;
  3763. INIT_LIST_HEAD(&obj->list);
  3764. INIT_LIST_HEAD(&obj->gpu_write_list);
  3765. obj->madv = I915_MADV_WILLNEED;
  3766. trace_i915_gem_object_create(&obj->base);
  3767. return &obj->base;
  3768. }
  3769. int i915_gem_init_object(struct drm_gem_object *obj)
  3770. {
  3771. BUG();
  3772. return 0;
  3773. }
  3774. void i915_gem_free_object(struct drm_gem_object *obj)
  3775. {
  3776. struct drm_device *dev = obj->dev;
  3777. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  3778. trace_i915_gem_object_destroy(obj);
  3779. while (obj_priv->pin_count > 0)
  3780. i915_gem_object_unpin(obj);
  3781. if (obj_priv->phys_obj)
  3782. i915_gem_detach_phys_object(dev, obj);
  3783. i915_gem_object_unbind(obj);
  3784. if (obj_priv->mmap_offset)
  3785. i915_gem_free_mmap_offset(obj);
  3786. drm_gem_object_release(obj);
  3787. kfree(obj_priv->page_cpu_valid);
  3788. kfree(obj_priv->bit_17);
  3789. kfree(obj_priv);
  3790. }
  3791. /** Unbinds all inactive objects. */
  3792. static int
  3793. i915_gem_evict_from_inactive_list(struct drm_device *dev)
  3794. {
  3795. drm_i915_private_t *dev_priv = dev->dev_private;
  3796. while (!list_empty(&dev_priv->mm.inactive_list)) {
  3797. struct drm_gem_object *obj;
  3798. int ret;
  3799. obj = &list_first_entry(&dev_priv->mm.inactive_list,
  3800. struct drm_i915_gem_object,
  3801. list)->base;
  3802. ret = i915_gem_object_unbind(obj);
  3803. if (ret != 0) {
  3804. DRM_ERROR("Error unbinding object: %d\n", ret);
  3805. return ret;
  3806. }
  3807. }
  3808. return 0;
  3809. }
  3810. int
  3811. i915_gem_idle(struct drm_device *dev)
  3812. {
  3813. drm_i915_private_t *dev_priv = dev->dev_private;
  3814. int ret;
  3815. mutex_lock(&dev->struct_mutex);
  3816. if (dev_priv->mm.suspended ||
  3817. (dev_priv->render_ring.gem_object == NULL) ||
  3818. (HAS_BSD(dev) &&
  3819. dev_priv->bsd_ring.gem_object == NULL)) {
  3820. mutex_unlock(&dev->struct_mutex);
  3821. return 0;
  3822. }
  3823. ret = i915_gpu_idle(dev);
  3824. if (ret) {
  3825. mutex_unlock(&dev->struct_mutex);
  3826. return ret;
  3827. }
  3828. /* Under UMS, be paranoid and evict. */
  3829. if (!drm_core_check_feature(dev, DRIVER_MODESET)) {
  3830. ret = i915_gem_evict_from_inactive_list(dev);
  3831. if (ret) {
  3832. mutex_unlock(&dev->struct_mutex);
  3833. return ret;
  3834. }
  3835. }
  3836. /* Hack! Don't let anybody do execbuf while we don't control the chip.
  3837. * We need to replace this with a semaphore, or something.
  3838. * And not confound mm.suspended!
  3839. */
  3840. dev_priv->mm.suspended = 1;
  3841. del_timer(&dev_priv->hangcheck_timer);
  3842. i915_kernel_lost_context(dev);
  3843. i915_gem_cleanup_ringbuffer(dev);
  3844. mutex_unlock(&dev->struct_mutex);
  3845. /* Cancel the retire work handler, which should be idle now. */
  3846. cancel_delayed_work_sync(&dev_priv->mm.retire_work);
  3847. return 0;
  3848. }
  3849. /*
  3850. * 965+ support PIPE_CONTROL commands, which provide finer grained control
  3851. * over cache flushing.
  3852. */
  3853. static int
  3854. i915_gem_init_pipe_control(struct drm_device *dev)
  3855. {
  3856. drm_i915_private_t *dev_priv = dev->dev_private;
  3857. struct drm_gem_object *obj;
  3858. struct drm_i915_gem_object *obj_priv;
  3859. int ret;
  3860. obj = i915_gem_alloc_object(dev, 4096);
  3861. if (obj == NULL) {
  3862. DRM_ERROR("Failed to allocate seqno page\n");
  3863. ret = -ENOMEM;
  3864. goto err;
  3865. }
  3866. obj_priv = to_intel_bo(obj);
  3867. obj_priv->agp_type = AGP_USER_CACHED_MEMORY;
  3868. ret = i915_gem_object_pin(obj, 4096);
  3869. if (ret)
  3870. goto err_unref;
  3871. dev_priv->seqno_gfx_addr = obj_priv->gtt_offset;
  3872. dev_priv->seqno_page = kmap(obj_priv->pages[0]);
  3873. if (dev_priv->seqno_page == NULL)
  3874. goto err_unpin;
  3875. dev_priv->seqno_obj = obj;
  3876. memset(dev_priv->seqno_page, 0, PAGE_SIZE);
  3877. return 0;
  3878. err_unpin:
  3879. i915_gem_object_unpin(obj);
  3880. err_unref:
  3881. drm_gem_object_unreference(obj);
  3882. err:
  3883. return ret;
  3884. }
  3885. static void
  3886. i915_gem_cleanup_pipe_control(struct drm_device *dev)
  3887. {
  3888. drm_i915_private_t *dev_priv = dev->dev_private;
  3889. struct drm_gem_object *obj;
  3890. struct drm_i915_gem_object *obj_priv;
  3891. obj = dev_priv->seqno_obj;
  3892. obj_priv = to_intel_bo(obj);
  3893. kunmap(obj_priv->pages[0]);
  3894. i915_gem_object_unpin(obj);
  3895. drm_gem_object_unreference(obj);
  3896. dev_priv->seqno_obj = NULL;
  3897. dev_priv->seqno_page = NULL;
  3898. }
  3899. int
  3900. i915_gem_init_ringbuffer(struct drm_device *dev)
  3901. {
  3902. drm_i915_private_t *dev_priv = dev->dev_private;
  3903. int ret;
  3904. dev_priv->render_ring = render_ring;
  3905. if (!I915_NEED_GFX_HWS(dev)) {
  3906. dev_priv->render_ring.status_page.page_addr
  3907. = dev_priv->status_page_dmah->vaddr;
  3908. memset(dev_priv->render_ring.status_page.page_addr,
  3909. 0, PAGE_SIZE);
  3910. }
  3911. if (HAS_PIPE_CONTROL(dev)) {
  3912. ret = i915_gem_init_pipe_control(dev);
  3913. if (ret)
  3914. return ret;
  3915. }
  3916. ret = intel_init_ring_buffer(dev, &dev_priv->render_ring);
  3917. if (ret)
  3918. goto cleanup_pipe_control;
  3919. if (HAS_BSD(dev)) {
  3920. dev_priv->bsd_ring = bsd_ring;
  3921. ret = intel_init_ring_buffer(dev, &dev_priv->bsd_ring);
  3922. if (ret)
  3923. goto cleanup_render_ring;
  3924. }
  3925. return 0;
  3926. cleanup_render_ring:
  3927. intel_cleanup_ring_buffer(dev, &dev_priv->render_ring);
  3928. cleanup_pipe_control:
  3929. if (HAS_PIPE_CONTROL(dev))
  3930. i915_gem_cleanup_pipe_control(dev);
  3931. return ret;
  3932. }
  3933. void
  3934. i915_gem_cleanup_ringbuffer(struct drm_device *dev)
  3935. {
  3936. drm_i915_private_t *dev_priv = dev->dev_private;
  3937. intel_cleanup_ring_buffer(dev, &dev_priv->render_ring);
  3938. if (HAS_BSD(dev))
  3939. intel_cleanup_ring_buffer(dev, &dev_priv->bsd_ring);
  3940. if (HAS_PIPE_CONTROL(dev))
  3941. i915_gem_cleanup_pipe_control(dev);
  3942. }
  3943. int
  3944. i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
  3945. struct drm_file *file_priv)
  3946. {
  3947. drm_i915_private_t *dev_priv = dev->dev_private;
  3948. int ret;
  3949. if (drm_core_check_feature(dev, DRIVER_MODESET))
  3950. return 0;
  3951. if (atomic_read(&dev_priv->mm.wedged)) {
  3952. DRM_ERROR("Reenabling wedged hardware, good luck\n");
  3953. atomic_set(&dev_priv->mm.wedged, 0);
  3954. }
  3955. mutex_lock(&dev->struct_mutex);
  3956. dev_priv->mm.suspended = 0;
  3957. ret = i915_gem_init_ringbuffer(dev);
  3958. if (ret != 0) {
  3959. mutex_unlock(&dev->struct_mutex);
  3960. return ret;
  3961. }
  3962. spin_lock(&dev_priv->mm.active_list_lock);
  3963. BUG_ON(!list_empty(&dev_priv->render_ring.active_list));
  3964. BUG_ON(HAS_BSD(dev) && !list_empty(&dev_priv->bsd_ring.active_list));
  3965. spin_unlock(&dev_priv->mm.active_list_lock);
  3966. BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
  3967. BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
  3968. BUG_ON(!list_empty(&dev_priv->render_ring.request_list));
  3969. BUG_ON(HAS_BSD(dev) && !list_empty(&dev_priv->bsd_ring.request_list));
  3970. mutex_unlock(&dev->struct_mutex);
  3971. drm_irq_install(dev);
  3972. return 0;
  3973. }
  3974. int
  3975. i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
  3976. struct drm_file *file_priv)
  3977. {
  3978. if (drm_core_check_feature(dev, DRIVER_MODESET))
  3979. return 0;
  3980. drm_irq_uninstall(dev);
  3981. return i915_gem_idle(dev);
  3982. }
  3983. void
  3984. i915_gem_lastclose(struct drm_device *dev)
  3985. {
  3986. int ret;
  3987. if (drm_core_check_feature(dev, DRIVER_MODESET))
  3988. return;
  3989. ret = i915_gem_idle(dev);
  3990. if (ret)
  3991. DRM_ERROR("failed to idle hardware: %d\n", ret);
  3992. }
  3993. void
  3994. i915_gem_load(struct drm_device *dev)
  3995. {
  3996. int i;
  3997. drm_i915_private_t *dev_priv = dev->dev_private;
  3998. spin_lock_init(&dev_priv->mm.active_list_lock);
  3999. INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
  4000. INIT_LIST_HEAD(&dev_priv->mm.gpu_write_list);
  4001. INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
  4002. INIT_LIST_HEAD(&dev_priv->mm.fence_list);
  4003. INIT_LIST_HEAD(&dev_priv->render_ring.active_list);
  4004. INIT_LIST_HEAD(&dev_priv->render_ring.request_list);
  4005. if (HAS_BSD(dev)) {
  4006. INIT_LIST_HEAD(&dev_priv->bsd_ring.active_list);
  4007. INIT_LIST_HEAD(&dev_priv->bsd_ring.request_list);
  4008. }
  4009. for (i = 0; i < 16; i++)
  4010. INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
  4011. INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
  4012. i915_gem_retire_work_handler);
  4013. spin_lock(&shrink_list_lock);
  4014. list_add(&dev_priv->mm.shrink_list, &shrink_list);
  4015. spin_unlock(&shrink_list_lock);
  4016. /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
  4017. if (IS_GEN3(dev)) {
  4018. u32 tmp = I915_READ(MI_ARB_STATE);
  4019. if (!(tmp & MI_ARB_C3_LP_WRITE_ENABLE)) {
  4020. /* arb state is a masked write, so set bit + bit in mask */
  4021. tmp = MI_ARB_C3_LP_WRITE_ENABLE | (MI_ARB_C3_LP_WRITE_ENABLE << MI_ARB_MASK_SHIFT);
  4022. I915_WRITE(MI_ARB_STATE, tmp);
  4023. }
  4024. }
  4025. /* Old X drivers will take 0-2 for front, back, depth buffers */
  4026. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  4027. dev_priv->fence_reg_start = 3;
  4028. if (IS_I965G(dev) || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
  4029. dev_priv->num_fence_regs = 16;
  4030. else
  4031. dev_priv->num_fence_regs = 8;
  4032. /* Initialize fence registers to zero */
  4033. if (IS_I965G(dev)) {
  4034. for (i = 0; i < 16; i++)
  4035. I915_WRITE64(FENCE_REG_965_0 + (i * 8), 0);
  4036. } else {
  4037. for (i = 0; i < 8; i++)
  4038. I915_WRITE(FENCE_REG_830_0 + (i * 4), 0);
  4039. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
  4040. for (i = 0; i < 8; i++)
  4041. I915_WRITE(FENCE_REG_945_8 + (i * 4), 0);
  4042. }
  4043. i915_gem_detect_bit_6_swizzle(dev);
  4044. init_waitqueue_head(&dev_priv->pending_flip_queue);
  4045. }
  4046. /*
  4047. * Create a physically contiguous memory object for this object
  4048. * e.g. for cursor + overlay regs
  4049. */
  4050. int i915_gem_init_phys_object(struct drm_device *dev,
  4051. int id, int size)
  4052. {
  4053. drm_i915_private_t *dev_priv = dev->dev_private;
  4054. struct drm_i915_gem_phys_object *phys_obj;
  4055. int ret;
  4056. if (dev_priv->mm.phys_objs[id - 1] || !size)
  4057. return 0;
  4058. phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
  4059. if (!phys_obj)
  4060. return -ENOMEM;
  4061. phys_obj->id = id;
  4062. phys_obj->handle = drm_pci_alloc(dev, size, 0);
  4063. if (!phys_obj->handle) {
  4064. ret = -ENOMEM;
  4065. goto kfree_obj;
  4066. }
  4067. #ifdef CONFIG_X86
  4068. set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
  4069. #endif
  4070. dev_priv->mm.phys_objs[id - 1] = phys_obj;
  4071. return 0;
  4072. kfree_obj:
  4073. kfree(phys_obj);
  4074. return ret;
  4075. }
  4076. void i915_gem_free_phys_object(struct drm_device *dev, int id)
  4077. {
  4078. drm_i915_private_t *dev_priv = dev->dev_private;
  4079. struct drm_i915_gem_phys_object *phys_obj;
  4080. if (!dev_priv->mm.phys_objs[id - 1])
  4081. return;
  4082. phys_obj = dev_priv->mm.phys_objs[id - 1];
  4083. if (phys_obj->cur_obj) {
  4084. i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
  4085. }
  4086. #ifdef CONFIG_X86
  4087. set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
  4088. #endif
  4089. drm_pci_free(dev, phys_obj->handle);
  4090. kfree(phys_obj);
  4091. dev_priv->mm.phys_objs[id - 1] = NULL;
  4092. }
  4093. void i915_gem_free_all_phys_object(struct drm_device *dev)
  4094. {
  4095. int i;
  4096. for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
  4097. i915_gem_free_phys_object(dev, i);
  4098. }
  4099. void i915_gem_detach_phys_object(struct drm_device *dev,
  4100. struct drm_gem_object *obj)
  4101. {
  4102. struct drm_i915_gem_object *obj_priv;
  4103. int i;
  4104. int ret;
  4105. int page_count;
  4106. obj_priv = to_intel_bo(obj);
  4107. if (!obj_priv->phys_obj)
  4108. return;
  4109. ret = i915_gem_object_get_pages(obj, 0);
  4110. if (ret)
  4111. goto out;
  4112. page_count = obj->size / PAGE_SIZE;
  4113. for (i = 0; i < page_count; i++) {
  4114. char *dst = kmap_atomic(obj_priv->pages[i], KM_USER0);
  4115. char *src = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
  4116. memcpy(dst, src, PAGE_SIZE);
  4117. kunmap_atomic(dst, KM_USER0);
  4118. }
  4119. drm_clflush_pages(obj_priv->pages, page_count);
  4120. drm_agp_chipset_flush(dev);
  4121. i915_gem_object_put_pages(obj);
  4122. out:
  4123. obj_priv->phys_obj->cur_obj = NULL;
  4124. obj_priv->phys_obj = NULL;
  4125. }
  4126. int
  4127. i915_gem_attach_phys_object(struct drm_device *dev,
  4128. struct drm_gem_object *obj, int id)
  4129. {
  4130. drm_i915_private_t *dev_priv = dev->dev_private;
  4131. struct drm_i915_gem_object *obj_priv;
  4132. int ret = 0;
  4133. int page_count;
  4134. int i;
  4135. if (id > I915_MAX_PHYS_OBJECT)
  4136. return -EINVAL;
  4137. obj_priv = to_intel_bo(obj);
  4138. if (obj_priv->phys_obj) {
  4139. if (obj_priv->phys_obj->id == id)
  4140. return 0;
  4141. i915_gem_detach_phys_object(dev, obj);
  4142. }
  4143. /* create a new object */
  4144. if (!dev_priv->mm.phys_objs[id - 1]) {
  4145. ret = i915_gem_init_phys_object(dev, id,
  4146. obj->size);
  4147. if (ret) {
  4148. DRM_ERROR("failed to init phys object %d size: %zu\n", id, obj->size);
  4149. goto out;
  4150. }
  4151. }
  4152. /* bind to the object */
  4153. obj_priv->phys_obj = dev_priv->mm.phys_objs[id - 1];
  4154. obj_priv->phys_obj->cur_obj = obj;
  4155. ret = i915_gem_object_get_pages(obj, 0);
  4156. if (ret) {
  4157. DRM_ERROR("failed to get page list\n");
  4158. goto out;
  4159. }
  4160. page_count = obj->size / PAGE_SIZE;
  4161. for (i = 0; i < page_count; i++) {
  4162. char *src = kmap_atomic(obj_priv->pages[i], KM_USER0);
  4163. char *dst = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
  4164. memcpy(dst, src, PAGE_SIZE);
  4165. kunmap_atomic(src, KM_USER0);
  4166. }
  4167. i915_gem_object_put_pages(obj);
  4168. return 0;
  4169. out:
  4170. return ret;
  4171. }
  4172. static int
  4173. i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
  4174. struct drm_i915_gem_pwrite *args,
  4175. struct drm_file *file_priv)
  4176. {
  4177. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  4178. void *obj_addr;
  4179. int ret;
  4180. char __user *user_data;
  4181. user_data = (char __user *) (uintptr_t) args->data_ptr;
  4182. obj_addr = obj_priv->phys_obj->handle->vaddr + args->offset;
  4183. DRM_DEBUG_DRIVER("obj_addr %p, %lld\n", obj_addr, args->size);
  4184. ret = copy_from_user(obj_addr, user_data, args->size);
  4185. if (ret)
  4186. return -EFAULT;
  4187. drm_agp_chipset_flush(dev);
  4188. return 0;
  4189. }
  4190. void i915_gem_release(struct drm_device * dev, struct drm_file *file_priv)
  4191. {
  4192. struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
  4193. /* Clean up our request list when the client is going away, so that
  4194. * later retire_requests won't dereference our soon-to-be-gone
  4195. * file_priv.
  4196. */
  4197. mutex_lock(&dev->struct_mutex);
  4198. while (!list_empty(&i915_file_priv->mm.request_list))
  4199. list_del_init(i915_file_priv->mm.request_list.next);
  4200. mutex_unlock(&dev->struct_mutex);
  4201. }
  4202. static int
  4203. i915_gpu_is_active(struct drm_device *dev)
  4204. {
  4205. drm_i915_private_t *dev_priv = dev->dev_private;
  4206. int lists_empty;
  4207. spin_lock(&dev_priv->mm.active_list_lock);
  4208. lists_empty = list_empty(&dev_priv->mm.flushing_list) &&
  4209. list_empty(&dev_priv->render_ring.active_list);
  4210. if (HAS_BSD(dev))
  4211. lists_empty &= list_empty(&dev_priv->bsd_ring.active_list);
  4212. spin_unlock(&dev_priv->mm.active_list_lock);
  4213. return !lists_empty;
  4214. }
  4215. static int
  4216. i915_gem_shrink(struct shrinker *shrink, int nr_to_scan, gfp_t gfp_mask)
  4217. {
  4218. drm_i915_private_t *dev_priv, *next_dev;
  4219. struct drm_i915_gem_object *obj_priv, *next_obj;
  4220. int cnt = 0;
  4221. int would_deadlock = 1;
  4222. /* "fast-path" to count number of available objects */
  4223. if (nr_to_scan == 0) {
  4224. spin_lock(&shrink_list_lock);
  4225. list_for_each_entry(dev_priv, &shrink_list, mm.shrink_list) {
  4226. struct drm_device *dev = dev_priv->dev;
  4227. if (mutex_trylock(&dev->struct_mutex)) {
  4228. list_for_each_entry(obj_priv,
  4229. &dev_priv->mm.inactive_list,
  4230. list)
  4231. cnt++;
  4232. mutex_unlock(&dev->struct_mutex);
  4233. }
  4234. }
  4235. spin_unlock(&shrink_list_lock);
  4236. return (cnt / 100) * sysctl_vfs_cache_pressure;
  4237. }
  4238. spin_lock(&shrink_list_lock);
  4239. rescan:
  4240. /* first scan for clean buffers */
  4241. list_for_each_entry_safe(dev_priv, next_dev,
  4242. &shrink_list, mm.shrink_list) {
  4243. struct drm_device *dev = dev_priv->dev;
  4244. if (! mutex_trylock(&dev->struct_mutex))
  4245. continue;
  4246. spin_unlock(&shrink_list_lock);
  4247. i915_gem_retire_requests(dev, &dev_priv->render_ring);
  4248. if (HAS_BSD(dev))
  4249. i915_gem_retire_requests(dev, &dev_priv->bsd_ring);
  4250. list_for_each_entry_safe(obj_priv, next_obj,
  4251. &dev_priv->mm.inactive_list,
  4252. list) {
  4253. if (i915_gem_object_is_purgeable(obj_priv)) {
  4254. i915_gem_object_unbind(&obj_priv->base);
  4255. if (--nr_to_scan <= 0)
  4256. break;
  4257. }
  4258. }
  4259. spin_lock(&shrink_list_lock);
  4260. mutex_unlock(&dev->struct_mutex);
  4261. would_deadlock = 0;
  4262. if (nr_to_scan <= 0)
  4263. break;
  4264. }
  4265. /* second pass, evict/count anything still on the inactive list */
  4266. list_for_each_entry_safe(dev_priv, next_dev,
  4267. &shrink_list, mm.shrink_list) {
  4268. struct drm_device *dev = dev_priv->dev;
  4269. if (! mutex_trylock(&dev->struct_mutex))
  4270. continue;
  4271. spin_unlock(&shrink_list_lock);
  4272. list_for_each_entry_safe(obj_priv, next_obj,
  4273. &dev_priv->mm.inactive_list,
  4274. list) {
  4275. if (nr_to_scan > 0) {
  4276. i915_gem_object_unbind(&obj_priv->base);
  4277. nr_to_scan--;
  4278. } else
  4279. cnt++;
  4280. }
  4281. spin_lock(&shrink_list_lock);
  4282. mutex_unlock(&dev->struct_mutex);
  4283. would_deadlock = 0;
  4284. }
  4285. if (nr_to_scan) {
  4286. int active = 0;
  4287. /*
  4288. * We are desperate for pages, so as a last resort, wait
  4289. * for the GPU to finish and discard whatever we can.
  4290. * This has a dramatic impact to reduce the number of
  4291. * OOM-killer events whilst running the GPU aggressively.
  4292. */
  4293. list_for_each_entry(dev_priv, &shrink_list, mm.shrink_list) {
  4294. struct drm_device *dev = dev_priv->dev;
  4295. if (!mutex_trylock(&dev->struct_mutex))
  4296. continue;
  4297. spin_unlock(&shrink_list_lock);
  4298. if (i915_gpu_is_active(dev)) {
  4299. i915_gpu_idle(dev);
  4300. active++;
  4301. }
  4302. spin_lock(&shrink_list_lock);
  4303. mutex_unlock(&dev->struct_mutex);
  4304. }
  4305. if (active)
  4306. goto rescan;
  4307. }
  4308. spin_unlock(&shrink_list_lock);
  4309. if (would_deadlock)
  4310. return -1;
  4311. else if (cnt > 0)
  4312. return (cnt / 100) * sysctl_vfs_cache_pressure;
  4313. else
  4314. return 0;
  4315. }
  4316. static struct shrinker shrinker = {
  4317. .shrink = i915_gem_shrink,
  4318. .seeks = DEFAULT_SEEKS,
  4319. };
  4320. __init void
  4321. i915_gem_shrinker_init(void)
  4322. {
  4323. register_shrinker(&shrinker);
  4324. }
  4325. __exit void
  4326. i915_gem_shrinker_exit(void)
  4327. {
  4328. unregister_shrinker(&shrinker);
  4329. }