i915_gem.c 132 KB

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