i915_gem.c 129 KB

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