i915_gem.c 135 KB

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