i915_gem.c 135 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162
  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. continue;
  1883. }
  1884. }
  1885. /* If we didn't do any of the above, there's no single buffer
  1886. * large enough to swap out for the new one, so just evict
  1887. * everything and start again. (This should be rare.)
  1888. */
  1889. if (!list_empty (&dev_priv->mm.inactive_list))
  1890. return i915_gem_evict_from_inactive_list(dev);
  1891. else
  1892. return i915_gem_evict_everything(dev);
  1893. }
  1894. }
  1895. int
  1896. i915_gem_object_get_pages(struct drm_gem_object *obj,
  1897. gfp_t gfpmask)
  1898. {
  1899. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  1900. int page_count, i;
  1901. struct address_space *mapping;
  1902. struct inode *inode;
  1903. struct page *page;
  1904. int ret;
  1905. if (obj_priv->pages_refcount++ != 0)
  1906. return 0;
  1907. /* Get the list of pages out of our struct file. They'll be pinned
  1908. * at this point until we release them.
  1909. */
  1910. page_count = obj->size / PAGE_SIZE;
  1911. BUG_ON(obj_priv->pages != NULL);
  1912. obj_priv->pages = drm_calloc_large(page_count, sizeof(struct page *));
  1913. if (obj_priv->pages == NULL) {
  1914. obj_priv->pages_refcount--;
  1915. return -ENOMEM;
  1916. }
  1917. inode = obj->filp->f_path.dentry->d_inode;
  1918. mapping = inode->i_mapping;
  1919. for (i = 0; i < page_count; i++) {
  1920. page = read_cache_page_gfp(mapping, i,
  1921. mapping_gfp_mask (mapping) |
  1922. __GFP_COLD |
  1923. gfpmask);
  1924. if (IS_ERR(page)) {
  1925. ret = PTR_ERR(page);
  1926. i915_gem_object_put_pages(obj);
  1927. return ret;
  1928. }
  1929. obj_priv->pages[i] = page;
  1930. }
  1931. if (obj_priv->tiling_mode != I915_TILING_NONE)
  1932. i915_gem_object_do_bit_17_swizzle(obj);
  1933. return 0;
  1934. }
  1935. static void sandybridge_write_fence_reg(struct drm_i915_fence_reg *reg)
  1936. {
  1937. struct drm_gem_object *obj = reg->obj;
  1938. struct drm_device *dev = obj->dev;
  1939. drm_i915_private_t *dev_priv = dev->dev_private;
  1940. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  1941. int regnum = obj_priv->fence_reg;
  1942. uint64_t val;
  1943. val = (uint64_t)((obj_priv->gtt_offset + obj->size - 4096) &
  1944. 0xfffff000) << 32;
  1945. val |= obj_priv->gtt_offset & 0xfffff000;
  1946. val |= (uint64_t)((obj_priv->stride / 128) - 1) <<
  1947. SANDYBRIDGE_FENCE_PITCH_SHIFT;
  1948. if (obj_priv->tiling_mode == I915_TILING_Y)
  1949. val |= 1 << I965_FENCE_TILING_Y_SHIFT;
  1950. val |= I965_FENCE_REG_VALID;
  1951. I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + (regnum * 8), val);
  1952. }
  1953. static void i965_write_fence_reg(struct drm_i915_fence_reg *reg)
  1954. {
  1955. struct drm_gem_object *obj = reg->obj;
  1956. struct drm_device *dev = obj->dev;
  1957. drm_i915_private_t *dev_priv = dev->dev_private;
  1958. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  1959. int regnum = obj_priv->fence_reg;
  1960. uint64_t val;
  1961. val = (uint64_t)((obj_priv->gtt_offset + obj->size - 4096) &
  1962. 0xfffff000) << 32;
  1963. val |= obj_priv->gtt_offset & 0xfffff000;
  1964. val |= ((obj_priv->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
  1965. if (obj_priv->tiling_mode == I915_TILING_Y)
  1966. val |= 1 << I965_FENCE_TILING_Y_SHIFT;
  1967. val |= I965_FENCE_REG_VALID;
  1968. I915_WRITE64(FENCE_REG_965_0 + (regnum * 8), val);
  1969. }
  1970. static void i915_write_fence_reg(struct drm_i915_fence_reg *reg)
  1971. {
  1972. struct drm_gem_object *obj = reg->obj;
  1973. struct drm_device *dev = obj->dev;
  1974. drm_i915_private_t *dev_priv = dev->dev_private;
  1975. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  1976. int regnum = obj_priv->fence_reg;
  1977. int tile_width;
  1978. uint32_t fence_reg, val;
  1979. uint32_t pitch_val;
  1980. if ((obj_priv->gtt_offset & ~I915_FENCE_START_MASK) ||
  1981. (obj_priv->gtt_offset & (obj->size - 1))) {
  1982. WARN(1, "%s: object 0x%08x not 1M or size (0x%zx) aligned\n",
  1983. __func__, obj_priv->gtt_offset, obj->size);
  1984. return;
  1985. }
  1986. if (obj_priv->tiling_mode == I915_TILING_Y &&
  1987. HAS_128_BYTE_Y_TILING(dev))
  1988. tile_width = 128;
  1989. else
  1990. tile_width = 512;
  1991. /* Note: pitch better be a power of two tile widths */
  1992. pitch_val = obj_priv->stride / tile_width;
  1993. pitch_val = ffs(pitch_val) - 1;
  1994. val = obj_priv->gtt_offset;
  1995. if (obj_priv->tiling_mode == I915_TILING_Y)
  1996. val |= 1 << I830_FENCE_TILING_Y_SHIFT;
  1997. val |= I915_FENCE_SIZE_BITS(obj->size);
  1998. val |= pitch_val << I830_FENCE_PITCH_SHIFT;
  1999. val |= I830_FENCE_REG_VALID;
  2000. if (regnum < 8)
  2001. fence_reg = FENCE_REG_830_0 + (regnum * 4);
  2002. else
  2003. fence_reg = FENCE_REG_945_8 + ((regnum - 8) * 4);
  2004. I915_WRITE(fence_reg, val);
  2005. }
  2006. static void i830_write_fence_reg(struct drm_i915_fence_reg *reg)
  2007. {
  2008. struct drm_gem_object *obj = reg->obj;
  2009. struct drm_device *dev = obj->dev;
  2010. drm_i915_private_t *dev_priv = dev->dev_private;
  2011. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  2012. int regnum = obj_priv->fence_reg;
  2013. uint32_t val;
  2014. uint32_t pitch_val;
  2015. uint32_t fence_size_bits;
  2016. if ((obj_priv->gtt_offset & ~I830_FENCE_START_MASK) ||
  2017. (obj_priv->gtt_offset & (obj->size - 1))) {
  2018. WARN(1, "%s: object 0x%08x not 512K or size aligned\n",
  2019. __func__, obj_priv->gtt_offset);
  2020. return;
  2021. }
  2022. pitch_val = obj_priv->stride / 128;
  2023. pitch_val = ffs(pitch_val) - 1;
  2024. WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
  2025. val = obj_priv->gtt_offset;
  2026. if (obj_priv->tiling_mode == I915_TILING_Y)
  2027. val |= 1 << I830_FENCE_TILING_Y_SHIFT;
  2028. fence_size_bits = I830_FENCE_SIZE_BITS(obj->size);
  2029. WARN_ON(fence_size_bits & ~0x00000f00);
  2030. val |= fence_size_bits;
  2031. val |= pitch_val << I830_FENCE_PITCH_SHIFT;
  2032. val |= I830_FENCE_REG_VALID;
  2033. I915_WRITE(FENCE_REG_830_0 + (regnum * 4), val);
  2034. }
  2035. static int i915_find_fence_reg(struct drm_device *dev)
  2036. {
  2037. struct drm_i915_fence_reg *reg = NULL;
  2038. struct drm_i915_gem_object *obj_priv = NULL;
  2039. struct drm_i915_private *dev_priv = dev->dev_private;
  2040. struct drm_gem_object *obj = NULL;
  2041. int i, avail, ret;
  2042. /* First try to find a free reg */
  2043. avail = 0;
  2044. for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
  2045. reg = &dev_priv->fence_regs[i];
  2046. if (!reg->obj)
  2047. return i;
  2048. obj_priv = reg->obj->driver_private;
  2049. if (!obj_priv->pin_count)
  2050. avail++;
  2051. }
  2052. if (avail == 0)
  2053. return -ENOSPC;
  2054. /* None available, try to steal one or wait for a user to finish */
  2055. i = I915_FENCE_REG_NONE;
  2056. list_for_each_entry(obj_priv, &dev_priv->mm.fence_list,
  2057. fence_list) {
  2058. obj = obj_priv->obj;
  2059. if (obj_priv->pin_count)
  2060. continue;
  2061. /* found one! */
  2062. i = obj_priv->fence_reg;
  2063. break;
  2064. }
  2065. BUG_ON(i == I915_FENCE_REG_NONE);
  2066. /* We only have a reference on obj from the active list. put_fence_reg
  2067. * might drop that one, causing a use-after-free in it. So hold a
  2068. * private reference to obj like the other callers of put_fence_reg
  2069. * (set_tiling ioctl) do. */
  2070. drm_gem_object_reference(obj);
  2071. ret = i915_gem_object_put_fence_reg(obj);
  2072. drm_gem_object_unreference(obj);
  2073. if (ret != 0)
  2074. return ret;
  2075. return i;
  2076. }
  2077. /**
  2078. * i915_gem_object_get_fence_reg - set up a fence reg for an object
  2079. * @obj: object to map through a fence reg
  2080. *
  2081. * When mapping objects through the GTT, userspace wants to be able to write
  2082. * to them without having to worry about swizzling if the object is tiled.
  2083. *
  2084. * This function walks the fence regs looking for a free one for @obj,
  2085. * stealing one if it can't find any.
  2086. *
  2087. * It then sets up the reg based on the object's properties: address, pitch
  2088. * and tiling format.
  2089. */
  2090. int
  2091. i915_gem_object_get_fence_reg(struct drm_gem_object *obj)
  2092. {
  2093. struct drm_device *dev = obj->dev;
  2094. struct drm_i915_private *dev_priv = dev->dev_private;
  2095. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  2096. struct drm_i915_fence_reg *reg = NULL;
  2097. int ret;
  2098. /* Just update our place in the LRU if our fence is getting used. */
  2099. if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
  2100. list_move_tail(&obj_priv->fence_list, &dev_priv->mm.fence_list);
  2101. return 0;
  2102. }
  2103. switch (obj_priv->tiling_mode) {
  2104. case I915_TILING_NONE:
  2105. WARN(1, "allocating a fence for non-tiled object?\n");
  2106. break;
  2107. case I915_TILING_X:
  2108. if (!obj_priv->stride)
  2109. return -EINVAL;
  2110. WARN((obj_priv->stride & (512 - 1)),
  2111. "object 0x%08x is X tiled but has non-512B pitch\n",
  2112. obj_priv->gtt_offset);
  2113. break;
  2114. case I915_TILING_Y:
  2115. if (!obj_priv->stride)
  2116. return -EINVAL;
  2117. WARN((obj_priv->stride & (128 - 1)),
  2118. "object 0x%08x is Y tiled but has non-128B pitch\n",
  2119. obj_priv->gtt_offset);
  2120. break;
  2121. }
  2122. ret = i915_find_fence_reg(dev);
  2123. if (ret < 0)
  2124. return ret;
  2125. obj_priv->fence_reg = ret;
  2126. reg = &dev_priv->fence_regs[obj_priv->fence_reg];
  2127. list_add_tail(&obj_priv->fence_list, &dev_priv->mm.fence_list);
  2128. reg->obj = obj;
  2129. if (IS_GEN6(dev))
  2130. sandybridge_write_fence_reg(reg);
  2131. else if (IS_I965G(dev))
  2132. i965_write_fence_reg(reg);
  2133. else if (IS_I9XX(dev))
  2134. i915_write_fence_reg(reg);
  2135. else
  2136. i830_write_fence_reg(reg);
  2137. trace_i915_gem_object_get_fence(obj, obj_priv->fence_reg,
  2138. obj_priv->tiling_mode);
  2139. return 0;
  2140. }
  2141. /**
  2142. * i915_gem_clear_fence_reg - clear out fence register info
  2143. * @obj: object to clear
  2144. *
  2145. * Zeroes out the fence register itself and clears out the associated
  2146. * data structures in dev_priv and obj_priv.
  2147. */
  2148. static void
  2149. i915_gem_clear_fence_reg(struct drm_gem_object *obj)
  2150. {
  2151. struct drm_device *dev = obj->dev;
  2152. drm_i915_private_t *dev_priv = dev->dev_private;
  2153. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  2154. if (IS_GEN6(dev)) {
  2155. I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 +
  2156. (obj_priv->fence_reg * 8), 0);
  2157. } else if (IS_I965G(dev)) {
  2158. I915_WRITE64(FENCE_REG_965_0 + (obj_priv->fence_reg * 8), 0);
  2159. } else {
  2160. uint32_t fence_reg;
  2161. if (obj_priv->fence_reg < 8)
  2162. fence_reg = FENCE_REG_830_0 + obj_priv->fence_reg * 4;
  2163. else
  2164. fence_reg = FENCE_REG_945_8 + (obj_priv->fence_reg -
  2165. 8) * 4;
  2166. I915_WRITE(fence_reg, 0);
  2167. }
  2168. dev_priv->fence_regs[obj_priv->fence_reg].obj = NULL;
  2169. obj_priv->fence_reg = I915_FENCE_REG_NONE;
  2170. list_del_init(&obj_priv->fence_list);
  2171. }
  2172. /**
  2173. * i915_gem_object_put_fence_reg - waits on outstanding fenced access
  2174. * to the buffer to finish, and then resets the fence register.
  2175. * @obj: tiled object holding a fence register.
  2176. *
  2177. * Zeroes out the fence register itself and clears out the associated
  2178. * data structures in dev_priv and obj_priv.
  2179. */
  2180. int
  2181. i915_gem_object_put_fence_reg(struct drm_gem_object *obj)
  2182. {
  2183. struct drm_device *dev = obj->dev;
  2184. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  2185. if (obj_priv->fence_reg == I915_FENCE_REG_NONE)
  2186. return 0;
  2187. /* If we've changed tiling, GTT-mappings of the object
  2188. * need to re-fault to ensure that the correct fence register
  2189. * setup is in place.
  2190. */
  2191. i915_gem_release_mmap(obj);
  2192. /* On the i915, GPU access to tiled buffers is via a fence,
  2193. * therefore we must wait for any outstanding access to complete
  2194. * before clearing the fence.
  2195. */
  2196. if (!IS_I965G(dev)) {
  2197. int ret;
  2198. i915_gem_object_flush_gpu_write_domain(obj);
  2199. ret = i915_gem_object_wait_rendering(obj);
  2200. if (ret != 0)
  2201. return ret;
  2202. }
  2203. i915_gem_object_flush_gtt_write_domain(obj);
  2204. i915_gem_clear_fence_reg (obj);
  2205. return 0;
  2206. }
  2207. /**
  2208. * Finds free space in the GTT aperture and binds the object there.
  2209. */
  2210. static int
  2211. i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment)
  2212. {
  2213. struct drm_device *dev = obj->dev;
  2214. drm_i915_private_t *dev_priv = dev->dev_private;
  2215. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  2216. struct drm_mm_node *free_space;
  2217. gfp_t gfpmask = __GFP_NORETRY | __GFP_NOWARN;
  2218. int ret;
  2219. if (obj_priv->madv != I915_MADV_WILLNEED) {
  2220. DRM_ERROR("Attempting to bind a purgeable object\n");
  2221. return -EINVAL;
  2222. }
  2223. if (alignment == 0)
  2224. alignment = i915_gem_get_gtt_alignment(obj);
  2225. if (alignment & (i915_gem_get_gtt_alignment(obj) - 1)) {
  2226. DRM_ERROR("Invalid object alignment requested %u\n", alignment);
  2227. return -EINVAL;
  2228. }
  2229. search_free:
  2230. free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
  2231. obj->size, alignment, 0);
  2232. if (free_space != NULL) {
  2233. obj_priv->gtt_space = drm_mm_get_block(free_space, obj->size,
  2234. alignment);
  2235. if (obj_priv->gtt_space != NULL) {
  2236. obj_priv->gtt_space->private = obj;
  2237. obj_priv->gtt_offset = obj_priv->gtt_space->start;
  2238. }
  2239. }
  2240. if (obj_priv->gtt_space == NULL) {
  2241. /* If the gtt is empty and we're still having trouble
  2242. * fitting our object in, we're out of memory.
  2243. */
  2244. #if WATCH_LRU
  2245. DRM_INFO("%s: GTT full, evicting something\n", __func__);
  2246. #endif
  2247. ret = i915_gem_evict_something(dev, obj->size);
  2248. if (ret)
  2249. return ret;
  2250. goto search_free;
  2251. }
  2252. #if WATCH_BUF
  2253. DRM_INFO("Binding object of size %zd at 0x%08x\n",
  2254. obj->size, obj_priv->gtt_offset);
  2255. #endif
  2256. ret = i915_gem_object_get_pages(obj, gfpmask);
  2257. if (ret) {
  2258. drm_mm_put_block(obj_priv->gtt_space);
  2259. obj_priv->gtt_space = NULL;
  2260. if (ret == -ENOMEM) {
  2261. /* first try to clear up some space from the GTT */
  2262. ret = i915_gem_evict_something(dev, obj->size);
  2263. if (ret) {
  2264. /* now try to shrink everyone else */
  2265. if (gfpmask) {
  2266. gfpmask = 0;
  2267. goto search_free;
  2268. }
  2269. return ret;
  2270. }
  2271. goto search_free;
  2272. }
  2273. return ret;
  2274. }
  2275. /* Create an AGP memory structure pointing at our pages, and bind it
  2276. * into the GTT.
  2277. */
  2278. obj_priv->agp_mem = drm_agp_bind_pages(dev,
  2279. obj_priv->pages,
  2280. obj->size >> PAGE_SHIFT,
  2281. obj_priv->gtt_offset,
  2282. obj_priv->agp_type);
  2283. if (obj_priv->agp_mem == NULL) {
  2284. i915_gem_object_put_pages(obj);
  2285. drm_mm_put_block(obj_priv->gtt_space);
  2286. obj_priv->gtt_space = NULL;
  2287. ret = i915_gem_evict_something(dev, obj->size);
  2288. if (ret)
  2289. return ret;
  2290. goto search_free;
  2291. }
  2292. atomic_inc(&dev->gtt_count);
  2293. atomic_add(obj->size, &dev->gtt_memory);
  2294. /* Assert that the object is not currently in any GPU domain. As it
  2295. * wasn't in the GTT, there shouldn't be any way it could have been in
  2296. * a GPU cache
  2297. */
  2298. BUG_ON(obj->read_domains & I915_GEM_GPU_DOMAINS);
  2299. BUG_ON(obj->write_domain & I915_GEM_GPU_DOMAINS);
  2300. trace_i915_gem_object_bind(obj, obj_priv->gtt_offset);
  2301. return 0;
  2302. }
  2303. void
  2304. i915_gem_clflush_object(struct drm_gem_object *obj)
  2305. {
  2306. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  2307. /* If we don't have a page list set up, then we're not pinned
  2308. * to GPU, and we can ignore the cache flush because it'll happen
  2309. * again at bind time.
  2310. */
  2311. if (obj_priv->pages == NULL)
  2312. return;
  2313. trace_i915_gem_object_clflush(obj);
  2314. drm_clflush_pages(obj_priv->pages, obj->size / PAGE_SIZE);
  2315. }
  2316. /** Flushes any GPU write domain for the object if it's dirty. */
  2317. static void
  2318. i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj)
  2319. {
  2320. struct drm_device *dev = obj->dev;
  2321. uint32_t old_write_domain;
  2322. if ((obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
  2323. return;
  2324. /* Queue the GPU write cache flushing we need. */
  2325. old_write_domain = obj->write_domain;
  2326. i915_gem_flush(dev, 0, obj->write_domain);
  2327. (void) i915_add_request(dev, NULL, obj->write_domain);
  2328. BUG_ON(obj->write_domain);
  2329. trace_i915_gem_object_change_domain(obj,
  2330. obj->read_domains,
  2331. old_write_domain);
  2332. }
  2333. /** Flushes the GTT write domain for the object if it's dirty. */
  2334. static void
  2335. i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj)
  2336. {
  2337. uint32_t old_write_domain;
  2338. if (obj->write_domain != I915_GEM_DOMAIN_GTT)
  2339. return;
  2340. /* No actual flushing is required for the GTT write domain. Writes
  2341. * to it immediately go to main memory as far as we know, so there's
  2342. * no chipset flush. It also doesn't land in render cache.
  2343. */
  2344. old_write_domain = obj->write_domain;
  2345. obj->write_domain = 0;
  2346. trace_i915_gem_object_change_domain(obj,
  2347. obj->read_domains,
  2348. old_write_domain);
  2349. }
  2350. /** Flushes the CPU write domain for the object if it's dirty. */
  2351. static void
  2352. i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj)
  2353. {
  2354. struct drm_device *dev = obj->dev;
  2355. uint32_t old_write_domain;
  2356. if (obj->write_domain != I915_GEM_DOMAIN_CPU)
  2357. return;
  2358. i915_gem_clflush_object(obj);
  2359. drm_agp_chipset_flush(dev);
  2360. old_write_domain = obj->write_domain;
  2361. obj->write_domain = 0;
  2362. trace_i915_gem_object_change_domain(obj,
  2363. obj->read_domains,
  2364. old_write_domain);
  2365. }
  2366. void
  2367. i915_gem_object_flush_write_domain(struct drm_gem_object *obj)
  2368. {
  2369. switch (obj->write_domain) {
  2370. case I915_GEM_DOMAIN_GTT:
  2371. i915_gem_object_flush_gtt_write_domain(obj);
  2372. break;
  2373. case I915_GEM_DOMAIN_CPU:
  2374. i915_gem_object_flush_cpu_write_domain(obj);
  2375. break;
  2376. default:
  2377. i915_gem_object_flush_gpu_write_domain(obj);
  2378. break;
  2379. }
  2380. }
  2381. /**
  2382. * Moves a single object to the GTT read, and possibly write domain.
  2383. *
  2384. * This function returns when the move is complete, including waiting on
  2385. * flushes to occur.
  2386. */
  2387. int
  2388. i915_gem_object_set_to_gtt_domain(struct drm_gem_object *obj, int write)
  2389. {
  2390. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  2391. uint32_t old_write_domain, old_read_domains;
  2392. int ret;
  2393. /* Not valid to be called on unbound objects. */
  2394. if (obj_priv->gtt_space == NULL)
  2395. return -EINVAL;
  2396. i915_gem_object_flush_gpu_write_domain(obj);
  2397. /* Wait on any GPU rendering and flushing to occur. */
  2398. ret = i915_gem_object_wait_rendering(obj);
  2399. if (ret != 0)
  2400. return ret;
  2401. old_write_domain = obj->write_domain;
  2402. old_read_domains = obj->read_domains;
  2403. /* If we're writing through the GTT domain, then CPU and GPU caches
  2404. * will need to be invalidated at next use.
  2405. */
  2406. if (write)
  2407. obj->read_domains &= I915_GEM_DOMAIN_GTT;
  2408. i915_gem_object_flush_cpu_write_domain(obj);
  2409. /* It should now be out of any other write domains, and we can update
  2410. * the domain values for our changes.
  2411. */
  2412. BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
  2413. obj->read_domains |= I915_GEM_DOMAIN_GTT;
  2414. if (write) {
  2415. obj->write_domain = I915_GEM_DOMAIN_GTT;
  2416. obj_priv->dirty = 1;
  2417. }
  2418. trace_i915_gem_object_change_domain(obj,
  2419. old_read_domains,
  2420. old_write_domain);
  2421. return 0;
  2422. }
  2423. /*
  2424. * Prepare buffer for display plane. Use uninterruptible for possible flush
  2425. * wait, as in modesetting process we're not supposed to be interrupted.
  2426. */
  2427. int
  2428. i915_gem_object_set_to_display_plane(struct drm_gem_object *obj)
  2429. {
  2430. struct drm_device *dev = obj->dev;
  2431. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  2432. uint32_t old_write_domain, old_read_domains;
  2433. int ret;
  2434. /* Not valid to be called on unbound objects. */
  2435. if (obj_priv->gtt_space == NULL)
  2436. return -EINVAL;
  2437. i915_gem_object_flush_gpu_write_domain(obj);
  2438. /* Wait on any GPU rendering and flushing to occur. */
  2439. if (obj_priv->active) {
  2440. #if WATCH_BUF
  2441. DRM_INFO("%s: object %p wait for seqno %08x\n",
  2442. __func__, obj, obj_priv->last_rendering_seqno);
  2443. #endif
  2444. ret = i915_do_wait_request(dev, obj_priv->last_rendering_seqno, 0);
  2445. if (ret != 0)
  2446. return ret;
  2447. }
  2448. old_write_domain = obj->write_domain;
  2449. old_read_domains = obj->read_domains;
  2450. obj->read_domains &= I915_GEM_DOMAIN_GTT;
  2451. i915_gem_object_flush_cpu_write_domain(obj);
  2452. /* It should now be out of any other write domains, and we can update
  2453. * the domain values for our changes.
  2454. */
  2455. BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
  2456. obj->read_domains |= I915_GEM_DOMAIN_GTT;
  2457. obj->write_domain = I915_GEM_DOMAIN_GTT;
  2458. obj_priv->dirty = 1;
  2459. trace_i915_gem_object_change_domain(obj,
  2460. old_read_domains,
  2461. old_write_domain);
  2462. return 0;
  2463. }
  2464. /**
  2465. * Moves a single object to the CPU read, and possibly write domain.
  2466. *
  2467. * This function returns when the move is complete, including waiting on
  2468. * flushes to occur.
  2469. */
  2470. static int
  2471. i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj, int write)
  2472. {
  2473. uint32_t old_write_domain, old_read_domains;
  2474. int ret;
  2475. i915_gem_object_flush_gpu_write_domain(obj);
  2476. /* Wait on any GPU rendering and flushing to occur. */
  2477. ret = i915_gem_object_wait_rendering(obj);
  2478. if (ret != 0)
  2479. return ret;
  2480. i915_gem_object_flush_gtt_write_domain(obj);
  2481. /* If we have a partially-valid cache of the object in the CPU,
  2482. * finish invalidating it and free the per-page flags.
  2483. */
  2484. i915_gem_object_set_to_full_cpu_read_domain(obj);
  2485. old_write_domain = obj->write_domain;
  2486. old_read_domains = obj->read_domains;
  2487. /* Flush the CPU cache if it's still invalid. */
  2488. if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) {
  2489. i915_gem_clflush_object(obj);
  2490. obj->read_domains |= I915_GEM_DOMAIN_CPU;
  2491. }
  2492. /* It should now be out of any other write domains, and we can update
  2493. * the domain values for our changes.
  2494. */
  2495. BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
  2496. /* If we're writing through the CPU, then the GPU read domains will
  2497. * need to be invalidated at next use.
  2498. */
  2499. if (write) {
  2500. obj->read_domains &= I915_GEM_DOMAIN_CPU;
  2501. obj->write_domain = I915_GEM_DOMAIN_CPU;
  2502. }
  2503. trace_i915_gem_object_change_domain(obj,
  2504. old_read_domains,
  2505. old_write_domain);
  2506. return 0;
  2507. }
  2508. /*
  2509. * Set the next domain for the specified object. This
  2510. * may not actually perform the necessary flushing/invaliding though,
  2511. * as that may want to be batched with other set_domain operations
  2512. *
  2513. * This is (we hope) the only really tricky part of gem. The goal
  2514. * is fairly simple -- track which caches hold bits of the object
  2515. * and make sure they remain coherent. A few concrete examples may
  2516. * help to explain how it works. For shorthand, we use the notation
  2517. * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the
  2518. * a pair of read and write domain masks.
  2519. *
  2520. * Case 1: the batch buffer
  2521. *
  2522. * 1. Allocated
  2523. * 2. Written by CPU
  2524. * 3. Mapped to GTT
  2525. * 4. Read by GPU
  2526. * 5. Unmapped from GTT
  2527. * 6. Freed
  2528. *
  2529. * Let's take these a step at a time
  2530. *
  2531. * 1. Allocated
  2532. * Pages allocated from the kernel may still have
  2533. * cache contents, so we set them to (CPU, CPU) always.
  2534. * 2. Written by CPU (using pwrite)
  2535. * The pwrite function calls set_domain (CPU, CPU) and
  2536. * this function does nothing (as nothing changes)
  2537. * 3. Mapped by GTT
  2538. * This function asserts that the object is not
  2539. * currently in any GPU-based read or write domains
  2540. * 4. Read by GPU
  2541. * i915_gem_execbuffer calls set_domain (COMMAND, 0).
  2542. * As write_domain is zero, this function adds in the
  2543. * current read domains (CPU+COMMAND, 0).
  2544. * flush_domains is set to CPU.
  2545. * invalidate_domains is set to COMMAND
  2546. * clflush is run to get data out of the CPU caches
  2547. * then i915_dev_set_domain calls i915_gem_flush to
  2548. * emit an MI_FLUSH and drm_agp_chipset_flush
  2549. * 5. Unmapped from GTT
  2550. * i915_gem_object_unbind calls set_domain (CPU, CPU)
  2551. * flush_domains and invalidate_domains end up both zero
  2552. * so no flushing/invalidating happens
  2553. * 6. Freed
  2554. * yay, done
  2555. *
  2556. * Case 2: The shared render buffer
  2557. *
  2558. * 1. Allocated
  2559. * 2. Mapped to GTT
  2560. * 3. Read/written by GPU
  2561. * 4. set_domain to (CPU,CPU)
  2562. * 5. Read/written by CPU
  2563. * 6. Read/written by GPU
  2564. *
  2565. * 1. Allocated
  2566. * Same as last example, (CPU, CPU)
  2567. * 2. Mapped to GTT
  2568. * Nothing changes (assertions find that it is not in the GPU)
  2569. * 3. Read/written by GPU
  2570. * execbuffer calls set_domain (RENDER, RENDER)
  2571. * flush_domains gets CPU
  2572. * invalidate_domains gets GPU
  2573. * clflush (obj)
  2574. * MI_FLUSH and drm_agp_chipset_flush
  2575. * 4. set_domain (CPU, CPU)
  2576. * flush_domains gets GPU
  2577. * invalidate_domains gets CPU
  2578. * wait_rendering (obj) to make sure all drawing is complete.
  2579. * This will include an MI_FLUSH to get the data from GPU
  2580. * to memory
  2581. * clflush (obj) to invalidate the CPU cache
  2582. * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?)
  2583. * 5. Read/written by CPU
  2584. * cache lines are loaded and dirtied
  2585. * 6. Read written by GPU
  2586. * Same as last GPU access
  2587. *
  2588. * Case 3: The constant buffer
  2589. *
  2590. * 1. Allocated
  2591. * 2. Written by CPU
  2592. * 3. Read by GPU
  2593. * 4. Updated (written) by CPU again
  2594. * 5. Read by GPU
  2595. *
  2596. * 1. Allocated
  2597. * (CPU, CPU)
  2598. * 2. Written by CPU
  2599. * (CPU, CPU)
  2600. * 3. Read by GPU
  2601. * (CPU+RENDER, 0)
  2602. * flush_domains = CPU
  2603. * invalidate_domains = RENDER
  2604. * clflush (obj)
  2605. * MI_FLUSH
  2606. * drm_agp_chipset_flush
  2607. * 4. Updated (written) by CPU again
  2608. * (CPU, CPU)
  2609. * flush_domains = 0 (no previous write domain)
  2610. * invalidate_domains = 0 (no new read domains)
  2611. * 5. Read by GPU
  2612. * (CPU+RENDER, 0)
  2613. * flush_domains = CPU
  2614. * invalidate_domains = RENDER
  2615. * clflush (obj)
  2616. * MI_FLUSH
  2617. * drm_agp_chipset_flush
  2618. */
  2619. static void
  2620. i915_gem_object_set_to_gpu_domain(struct drm_gem_object *obj)
  2621. {
  2622. struct drm_device *dev = obj->dev;
  2623. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  2624. uint32_t invalidate_domains = 0;
  2625. uint32_t flush_domains = 0;
  2626. uint32_t old_read_domains;
  2627. BUG_ON(obj->pending_read_domains & I915_GEM_DOMAIN_CPU);
  2628. BUG_ON(obj->pending_write_domain == I915_GEM_DOMAIN_CPU);
  2629. intel_mark_busy(dev, obj);
  2630. #if WATCH_BUF
  2631. DRM_INFO("%s: object %p read %08x -> %08x write %08x -> %08x\n",
  2632. __func__, obj,
  2633. obj->read_domains, obj->pending_read_domains,
  2634. obj->write_domain, obj->pending_write_domain);
  2635. #endif
  2636. /*
  2637. * If the object isn't moving to a new write domain,
  2638. * let the object stay in multiple read domains
  2639. */
  2640. if (obj->pending_write_domain == 0)
  2641. obj->pending_read_domains |= obj->read_domains;
  2642. else
  2643. obj_priv->dirty = 1;
  2644. /*
  2645. * Flush the current write domain if
  2646. * the new read domains don't match. Invalidate
  2647. * any read domains which differ from the old
  2648. * write domain
  2649. */
  2650. if (obj->write_domain &&
  2651. obj->write_domain != obj->pending_read_domains) {
  2652. flush_domains |= obj->write_domain;
  2653. invalidate_domains |=
  2654. obj->pending_read_domains & ~obj->write_domain;
  2655. }
  2656. /*
  2657. * Invalidate any read caches which may have
  2658. * stale data. That is, any new read domains.
  2659. */
  2660. invalidate_domains |= obj->pending_read_domains & ~obj->read_domains;
  2661. if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU) {
  2662. #if WATCH_BUF
  2663. DRM_INFO("%s: CPU domain flush %08x invalidate %08x\n",
  2664. __func__, flush_domains, invalidate_domains);
  2665. #endif
  2666. i915_gem_clflush_object(obj);
  2667. }
  2668. old_read_domains = obj->read_domains;
  2669. /* The actual obj->write_domain will be updated with
  2670. * pending_write_domain after we emit the accumulated flush for all
  2671. * of our domain changes in execbuffers (which clears objects'
  2672. * write_domains). So if we have a current write domain that we
  2673. * aren't changing, set pending_write_domain to that.
  2674. */
  2675. if (flush_domains == 0 && obj->pending_write_domain == 0)
  2676. obj->pending_write_domain = obj->write_domain;
  2677. obj->read_domains = obj->pending_read_domains;
  2678. dev->invalidate_domains |= invalidate_domains;
  2679. dev->flush_domains |= flush_domains;
  2680. #if WATCH_BUF
  2681. DRM_INFO("%s: read %08x write %08x invalidate %08x flush %08x\n",
  2682. __func__,
  2683. obj->read_domains, obj->write_domain,
  2684. dev->invalidate_domains, dev->flush_domains);
  2685. #endif
  2686. trace_i915_gem_object_change_domain(obj,
  2687. old_read_domains,
  2688. obj->write_domain);
  2689. }
  2690. /**
  2691. * Moves the object from a partially CPU read to a full one.
  2692. *
  2693. * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(),
  2694. * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU).
  2695. */
  2696. static void
  2697. i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj)
  2698. {
  2699. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  2700. if (!obj_priv->page_cpu_valid)
  2701. return;
  2702. /* If we're partially in the CPU read domain, finish moving it in.
  2703. */
  2704. if (obj->read_domains & I915_GEM_DOMAIN_CPU) {
  2705. int i;
  2706. for (i = 0; i <= (obj->size - 1) / PAGE_SIZE; i++) {
  2707. if (obj_priv->page_cpu_valid[i])
  2708. continue;
  2709. drm_clflush_pages(obj_priv->pages + i, 1);
  2710. }
  2711. }
  2712. /* Free the page_cpu_valid mappings which are now stale, whether
  2713. * or not we've got I915_GEM_DOMAIN_CPU.
  2714. */
  2715. kfree(obj_priv->page_cpu_valid);
  2716. obj_priv->page_cpu_valid = NULL;
  2717. }
  2718. /**
  2719. * Set the CPU read domain on a range of the object.
  2720. *
  2721. * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's
  2722. * not entirely valid. The page_cpu_valid member of the object flags which
  2723. * pages have been flushed, and will be respected by
  2724. * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping
  2725. * of the whole object.
  2726. *
  2727. * This function returns when the move is complete, including waiting on
  2728. * flushes to occur.
  2729. */
  2730. static int
  2731. i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
  2732. uint64_t offset, uint64_t size)
  2733. {
  2734. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  2735. uint32_t old_read_domains;
  2736. int i, ret;
  2737. if (offset == 0 && size == obj->size)
  2738. return i915_gem_object_set_to_cpu_domain(obj, 0);
  2739. i915_gem_object_flush_gpu_write_domain(obj);
  2740. /* Wait on any GPU rendering and flushing to occur. */
  2741. ret = i915_gem_object_wait_rendering(obj);
  2742. if (ret != 0)
  2743. return ret;
  2744. i915_gem_object_flush_gtt_write_domain(obj);
  2745. /* If we're already fully in the CPU read domain, we're done. */
  2746. if (obj_priv->page_cpu_valid == NULL &&
  2747. (obj->read_domains & I915_GEM_DOMAIN_CPU) != 0)
  2748. return 0;
  2749. /* Otherwise, create/clear the per-page CPU read domain flag if we're
  2750. * newly adding I915_GEM_DOMAIN_CPU
  2751. */
  2752. if (obj_priv->page_cpu_valid == NULL) {
  2753. obj_priv->page_cpu_valid = kzalloc(obj->size / PAGE_SIZE,
  2754. GFP_KERNEL);
  2755. if (obj_priv->page_cpu_valid == NULL)
  2756. return -ENOMEM;
  2757. } else if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0)
  2758. memset(obj_priv->page_cpu_valid, 0, obj->size / PAGE_SIZE);
  2759. /* Flush the cache on any pages that are still invalid from the CPU's
  2760. * perspective.
  2761. */
  2762. for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE;
  2763. i++) {
  2764. if (obj_priv->page_cpu_valid[i])
  2765. continue;
  2766. drm_clflush_pages(obj_priv->pages + i, 1);
  2767. obj_priv->page_cpu_valid[i] = 1;
  2768. }
  2769. /* It should now be out of any other write domains, and we can update
  2770. * the domain values for our changes.
  2771. */
  2772. BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
  2773. old_read_domains = obj->read_domains;
  2774. obj->read_domains |= I915_GEM_DOMAIN_CPU;
  2775. trace_i915_gem_object_change_domain(obj,
  2776. old_read_domains,
  2777. obj->write_domain);
  2778. return 0;
  2779. }
  2780. /**
  2781. * Pin an object to the GTT and evaluate the relocations landing in it.
  2782. */
  2783. static int
  2784. i915_gem_object_pin_and_relocate(struct drm_gem_object *obj,
  2785. struct drm_file *file_priv,
  2786. struct drm_i915_gem_exec_object2 *entry,
  2787. struct drm_i915_gem_relocation_entry *relocs)
  2788. {
  2789. struct drm_device *dev = obj->dev;
  2790. drm_i915_private_t *dev_priv = dev->dev_private;
  2791. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  2792. int i, ret;
  2793. void __iomem *reloc_page;
  2794. bool need_fence;
  2795. need_fence = entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
  2796. obj_priv->tiling_mode != I915_TILING_NONE;
  2797. /* Check fence reg constraints and rebind if necessary */
  2798. if (need_fence && !i915_gem_object_fence_offset_ok(obj,
  2799. obj_priv->tiling_mode))
  2800. i915_gem_object_unbind(obj);
  2801. /* Choose the GTT offset for our buffer and put it there. */
  2802. ret = i915_gem_object_pin(obj, (uint32_t) entry->alignment);
  2803. if (ret)
  2804. return ret;
  2805. /*
  2806. * Pre-965 chips need a fence register set up in order to
  2807. * properly handle blits to/from tiled surfaces.
  2808. */
  2809. if (need_fence) {
  2810. ret = i915_gem_object_get_fence_reg(obj);
  2811. if (ret != 0) {
  2812. if (ret != -EBUSY && ret != -ERESTARTSYS)
  2813. DRM_ERROR("Failure to install fence: %d\n",
  2814. ret);
  2815. i915_gem_object_unpin(obj);
  2816. return ret;
  2817. }
  2818. }
  2819. entry->offset = obj_priv->gtt_offset;
  2820. /* Apply the relocations, using the GTT aperture to avoid cache
  2821. * flushing requirements.
  2822. */
  2823. for (i = 0; i < entry->relocation_count; i++) {
  2824. struct drm_i915_gem_relocation_entry *reloc= &relocs[i];
  2825. struct drm_gem_object *target_obj;
  2826. struct drm_i915_gem_object *target_obj_priv;
  2827. uint32_t reloc_val, reloc_offset;
  2828. uint32_t __iomem *reloc_entry;
  2829. target_obj = drm_gem_object_lookup(obj->dev, file_priv,
  2830. reloc->target_handle);
  2831. if (target_obj == NULL) {
  2832. i915_gem_object_unpin(obj);
  2833. return -EBADF;
  2834. }
  2835. target_obj_priv = target_obj->driver_private;
  2836. #if WATCH_RELOC
  2837. DRM_INFO("%s: obj %p offset %08x target %d "
  2838. "read %08x write %08x gtt %08x "
  2839. "presumed %08x delta %08x\n",
  2840. __func__,
  2841. obj,
  2842. (int) reloc->offset,
  2843. (int) reloc->target_handle,
  2844. (int) reloc->read_domains,
  2845. (int) reloc->write_domain,
  2846. (int) target_obj_priv->gtt_offset,
  2847. (int) reloc->presumed_offset,
  2848. reloc->delta);
  2849. #endif
  2850. /* The target buffer should have appeared before us in the
  2851. * exec_object list, so it should have a GTT space bound by now.
  2852. */
  2853. if (target_obj_priv->gtt_space == NULL) {
  2854. DRM_ERROR("No GTT space found for object %d\n",
  2855. reloc->target_handle);
  2856. drm_gem_object_unreference(target_obj);
  2857. i915_gem_object_unpin(obj);
  2858. return -EINVAL;
  2859. }
  2860. /* Validate that the target is in a valid r/w GPU domain */
  2861. if (reloc->write_domain & (reloc->write_domain - 1)) {
  2862. DRM_ERROR("reloc with multiple write domains: "
  2863. "obj %p target %d offset %d "
  2864. "read %08x write %08x",
  2865. obj, reloc->target_handle,
  2866. (int) reloc->offset,
  2867. reloc->read_domains,
  2868. reloc->write_domain);
  2869. return -EINVAL;
  2870. }
  2871. if (reloc->write_domain & I915_GEM_DOMAIN_CPU ||
  2872. reloc->read_domains & I915_GEM_DOMAIN_CPU) {
  2873. DRM_ERROR("reloc with read/write CPU domains: "
  2874. "obj %p target %d offset %d "
  2875. "read %08x write %08x",
  2876. obj, reloc->target_handle,
  2877. (int) reloc->offset,
  2878. reloc->read_domains,
  2879. reloc->write_domain);
  2880. drm_gem_object_unreference(target_obj);
  2881. i915_gem_object_unpin(obj);
  2882. return -EINVAL;
  2883. }
  2884. if (reloc->write_domain && target_obj->pending_write_domain &&
  2885. reloc->write_domain != target_obj->pending_write_domain) {
  2886. DRM_ERROR("Write domain conflict: "
  2887. "obj %p target %d offset %d "
  2888. "new %08x old %08x\n",
  2889. obj, reloc->target_handle,
  2890. (int) reloc->offset,
  2891. reloc->write_domain,
  2892. target_obj->pending_write_domain);
  2893. drm_gem_object_unreference(target_obj);
  2894. i915_gem_object_unpin(obj);
  2895. return -EINVAL;
  2896. }
  2897. target_obj->pending_read_domains |= reloc->read_domains;
  2898. target_obj->pending_write_domain |= reloc->write_domain;
  2899. /* If the relocation already has the right value in it, no
  2900. * more work needs to be done.
  2901. */
  2902. if (target_obj_priv->gtt_offset == reloc->presumed_offset) {
  2903. drm_gem_object_unreference(target_obj);
  2904. continue;
  2905. }
  2906. /* Check that the relocation address is valid... */
  2907. if (reloc->offset > obj->size - 4) {
  2908. DRM_ERROR("Relocation beyond object bounds: "
  2909. "obj %p target %d offset %d size %d.\n",
  2910. obj, reloc->target_handle,
  2911. (int) reloc->offset, (int) obj->size);
  2912. drm_gem_object_unreference(target_obj);
  2913. i915_gem_object_unpin(obj);
  2914. return -EINVAL;
  2915. }
  2916. if (reloc->offset & 3) {
  2917. DRM_ERROR("Relocation not 4-byte aligned: "
  2918. "obj %p target %d offset %d.\n",
  2919. obj, reloc->target_handle,
  2920. (int) reloc->offset);
  2921. drm_gem_object_unreference(target_obj);
  2922. i915_gem_object_unpin(obj);
  2923. return -EINVAL;
  2924. }
  2925. /* and points to somewhere within the target object. */
  2926. if (reloc->delta >= target_obj->size) {
  2927. DRM_ERROR("Relocation beyond target object bounds: "
  2928. "obj %p target %d delta %d size %d.\n",
  2929. obj, reloc->target_handle,
  2930. (int) reloc->delta, (int) target_obj->size);
  2931. drm_gem_object_unreference(target_obj);
  2932. i915_gem_object_unpin(obj);
  2933. return -EINVAL;
  2934. }
  2935. ret = i915_gem_object_set_to_gtt_domain(obj, 1);
  2936. if (ret != 0) {
  2937. drm_gem_object_unreference(target_obj);
  2938. i915_gem_object_unpin(obj);
  2939. return -EINVAL;
  2940. }
  2941. /* Map the page containing the relocation we're going to
  2942. * perform.
  2943. */
  2944. reloc_offset = obj_priv->gtt_offset + reloc->offset;
  2945. reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
  2946. (reloc_offset &
  2947. ~(PAGE_SIZE - 1)));
  2948. reloc_entry = (uint32_t __iomem *)(reloc_page +
  2949. (reloc_offset & (PAGE_SIZE - 1)));
  2950. reloc_val = target_obj_priv->gtt_offset + reloc->delta;
  2951. #if WATCH_BUF
  2952. DRM_INFO("Applied relocation: %p@0x%08x %08x -> %08x\n",
  2953. obj, (unsigned int) reloc->offset,
  2954. readl(reloc_entry), reloc_val);
  2955. #endif
  2956. writel(reloc_val, reloc_entry);
  2957. io_mapping_unmap_atomic(reloc_page);
  2958. /* The updated presumed offset for this entry will be
  2959. * copied back out to the user.
  2960. */
  2961. reloc->presumed_offset = target_obj_priv->gtt_offset;
  2962. drm_gem_object_unreference(target_obj);
  2963. }
  2964. #if WATCH_BUF
  2965. if (0)
  2966. i915_gem_dump_object(obj, 128, __func__, ~0);
  2967. #endif
  2968. return 0;
  2969. }
  2970. /** Dispatch a batchbuffer to the ring
  2971. */
  2972. static int
  2973. i915_dispatch_gem_execbuffer(struct drm_device *dev,
  2974. struct drm_i915_gem_execbuffer2 *exec,
  2975. struct drm_clip_rect *cliprects,
  2976. uint64_t exec_offset)
  2977. {
  2978. drm_i915_private_t *dev_priv = dev->dev_private;
  2979. int nbox = exec->num_cliprects;
  2980. int i = 0, count;
  2981. uint32_t exec_start, exec_len;
  2982. RING_LOCALS;
  2983. exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
  2984. exec_len = (uint32_t) exec->batch_len;
  2985. trace_i915_gem_request_submit(dev, dev_priv->mm.next_gem_seqno + 1);
  2986. count = nbox ? nbox : 1;
  2987. for (i = 0; i < count; i++) {
  2988. if (i < nbox) {
  2989. int ret = i915_emit_box(dev, cliprects, i,
  2990. exec->DR1, exec->DR4);
  2991. if (ret)
  2992. return ret;
  2993. }
  2994. if (IS_I830(dev) || IS_845G(dev)) {
  2995. BEGIN_LP_RING(4);
  2996. OUT_RING(MI_BATCH_BUFFER);
  2997. OUT_RING(exec_start | MI_BATCH_NON_SECURE);
  2998. OUT_RING(exec_start + exec_len - 4);
  2999. OUT_RING(0);
  3000. ADVANCE_LP_RING();
  3001. } else {
  3002. BEGIN_LP_RING(2);
  3003. if (IS_I965G(dev)) {
  3004. OUT_RING(MI_BATCH_BUFFER_START |
  3005. (2 << 6) |
  3006. MI_BATCH_NON_SECURE_I965);
  3007. OUT_RING(exec_start);
  3008. } else {
  3009. OUT_RING(MI_BATCH_BUFFER_START |
  3010. (2 << 6));
  3011. OUT_RING(exec_start | MI_BATCH_NON_SECURE);
  3012. }
  3013. ADVANCE_LP_RING();
  3014. }
  3015. }
  3016. /* XXX breadcrumb */
  3017. return 0;
  3018. }
  3019. /* Throttle our rendering by waiting until the ring has completed our requests
  3020. * emitted over 20 msec ago.
  3021. *
  3022. * Note that if we were to use the current jiffies each time around the loop,
  3023. * we wouldn't escape the function with any frames outstanding if the time to
  3024. * render a frame was over 20ms.
  3025. *
  3026. * This should get us reasonable parallelism between CPU and GPU but also
  3027. * relatively low latency when blocking on a particular request to finish.
  3028. */
  3029. static int
  3030. i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file_priv)
  3031. {
  3032. struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
  3033. int ret = 0;
  3034. unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
  3035. mutex_lock(&dev->struct_mutex);
  3036. while (!list_empty(&i915_file_priv->mm.request_list)) {
  3037. struct drm_i915_gem_request *request;
  3038. request = list_first_entry(&i915_file_priv->mm.request_list,
  3039. struct drm_i915_gem_request,
  3040. client_list);
  3041. if (time_after_eq(request->emitted_jiffies, recent_enough))
  3042. break;
  3043. ret = i915_wait_request(dev, request->seqno);
  3044. if (ret != 0)
  3045. break;
  3046. }
  3047. mutex_unlock(&dev->struct_mutex);
  3048. return ret;
  3049. }
  3050. static int
  3051. i915_gem_get_relocs_from_user(struct drm_i915_gem_exec_object2 *exec_list,
  3052. uint32_t buffer_count,
  3053. struct drm_i915_gem_relocation_entry **relocs)
  3054. {
  3055. uint32_t reloc_count = 0, reloc_index = 0, i;
  3056. int ret;
  3057. *relocs = NULL;
  3058. for (i = 0; i < buffer_count; i++) {
  3059. if (reloc_count + exec_list[i].relocation_count < reloc_count)
  3060. return -EINVAL;
  3061. reloc_count += exec_list[i].relocation_count;
  3062. }
  3063. *relocs = drm_calloc_large(reloc_count, sizeof(**relocs));
  3064. if (*relocs == NULL) {
  3065. DRM_ERROR("failed to alloc relocs, count %d\n", reloc_count);
  3066. return -ENOMEM;
  3067. }
  3068. for (i = 0; i < buffer_count; i++) {
  3069. struct drm_i915_gem_relocation_entry __user *user_relocs;
  3070. user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
  3071. ret = copy_from_user(&(*relocs)[reloc_index],
  3072. user_relocs,
  3073. exec_list[i].relocation_count *
  3074. sizeof(**relocs));
  3075. if (ret != 0) {
  3076. drm_free_large(*relocs);
  3077. *relocs = NULL;
  3078. return -EFAULT;
  3079. }
  3080. reloc_index += exec_list[i].relocation_count;
  3081. }
  3082. return 0;
  3083. }
  3084. static int
  3085. i915_gem_put_relocs_to_user(struct drm_i915_gem_exec_object2 *exec_list,
  3086. uint32_t buffer_count,
  3087. struct drm_i915_gem_relocation_entry *relocs)
  3088. {
  3089. uint32_t reloc_count = 0, i;
  3090. int ret = 0;
  3091. if (relocs == NULL)
  3092. return 0;
  3093. for (i = 0; i < buffer_count; i++) {
  3094. struct drm_i915_gem_relocation_entry __user *user_relocs;
  3095. int unwritten;
  3096. user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
  3097. unwritten = copy_to_user(user_relocs,
  3098. &relocs[reloc_count],
  3099. exec_list[i].relocation_count *
  3100. sizeof(*relocs));
  3101. if (unwritten) {
  3102. ret = -EFAULT;
  3103. goto err;
  3104. }
  3105. reloc_count += exec_list[i].relocation_count;
  3106. }
  3107. err:
  3108. drm_free_large(relocs);
  3109. return ret;
  3110. }
  3111. static int
  3112. i915_gem_check_execbuffer (struct drm_i915_gem_execbuffer2 *exec,
  3113. uint64_t exec_offset)
  3114. {
  3115. uint32_t exec_start, exec_len;
  3116. exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
  3117. exec_len = (uint32_t) exec->batch_len;
  3118. if ((exec_start | exec_len) & 0x7)
  3119. return -EINVAL;
  3120. if (!exec_start)
  3121. return -EINVAL;
  3122. return 0;
  3123. }
  3124. static int
  3125. i915_gem_wait_for_pending_flip(struct drm_device *dev,
  3126. struct drm_gem_object **object_list,
  3127. int count)
  3128. {
  3129. drm_i915_private_t *dev_priv = dev->dev_private;
  3130. struct drm_i915_gem_object *obj_priv;
  3131. DEFINE_WAIT(wait);
  3132. int i, ret = 0;
  3133. for (;;) {
  3134. prepare_to_wait(&dev_priv->pending_flip_queue,
  3135. &wait, TASK_INTERRUPTIBLE);
  3136. for (i = 0; i < count; i++) {
  3137. obj_priv = object_list[i]->driver_private;
  3138. if (atomic_read(&obj_priv->pending_flip) > 0)
  3139. break;
  3140. }
  3141. if (i == count)
  3142. break;
  3143. if (!signal_pending(current)) {
  3144. mutex_unlock(&dev->struct_mutex);
  3145. schedule();
  3146. mutex_lock(&dev->struct_mutex);
  3147. continue;
  3148. }
  3149. ret = -ERESTARTSYS;
  3150. break;
  3151. }
  3152. finish_wait(&dev_priv->pending_flip_queue, &wait);
  3153. return ret;
  3154. }
  3155. int
  3156. i915_gem_do_execbuffer(struct drm_device *dev, void *data,
  3157. struct drm_file *file_priv,
  3158. struct drm_i915_gem_execbuffer2 *args,
  3159. struct drm_i915_gem_exec_object2 *exec_list)
  3160. {
  3161. drm_i915_private_t *dev_priv = dev->dev_private;
  3162. struct drm_gem_object **object_list = NULL;
  3163. struct drm_gem_object *batch_obj;
  3164. struct drm_i915_gem_object *obj_priv;
  3165. struct drm_clip_rect *cliprects = NULL;
  3166. struct drm_i915_gem_relocation_entry *relocs = NULL;
  3167. int ret = 0, ret2, i, pinned = 0;
  3168. uint64_t exec_offset;
  3169. uint32_t seqno, flush_domains, reloc_index;
  3170. int pin_tries, flips;
  3171. #if WATCH_EXEC
  3172. DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
  3173. (int) args->buffers_ptr, args->buffer_count, args->batch_len);
  3174. #endif
  3175. if (args->buffer_count < 1) {
  3176. DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
  3177. return -EINVAL;
  3178. }
  3179. object_list = drm_malloc_ab(sizeof(*object_list), args->buffer_count);
  3180. if (object_list == NULL) {
  3181. DRM_ERROR("Failed to allocate object list for %d buffers\n",
  3182. args->buffer_count);
  3183. ret = -ENOMEM;
  3184. goto pre_mutex_err;
  3185. }
  3186. if (args->num_cliprects != 0) {
  3187. cliprects = kcalloc(args->num_cliprects, sizeof(*cliprects),
  3188. GFP_KERNEL);
  3189. if (cliprects == NULL) {
  3190. ret = -ENOMEM;
  3191. goto pre_mutex_err;
  3192. }
  3193. ret = copy_from_user(cliprects,
  3194. (struct drm_clip_rect __user *)
  3195. (uintptr_t) args->cliprects_ptr,
  3196. sizeof(*cliprects) * args->num_cliprects);
  3197. if (ret != 0) {
  3198. DRM_ERROR("copy %d cliprects failed: %d\n",
  3199. args->num_cliprects, ret);
  3200. goto pre_mutex_err;
  3201. }
  3202. }
  3203. ret = i915_gem_get_relocs_from_user(exec_list, args->buffer_count,
  3204. &relocs);
  3205. if (ret != 0)
  3206. goto pre_mutex_err;
  3207. mutex_lock(&dev->struct_mutex);
  3208. i915_verify_inactive(dev, __FILE__, __LINE__);
  3209. if (atomic_read(&dev_priv->mm.wedged)) {
  3210. mutex_unlock(&dev->struct_mutex);
  3211. ret = -EIO;
  3212. goto pre_mutex_err;
  3213. }
  3214. if (dev_priv->mm.suspended) {
  3215. mutex_unlock(&dev->struct_mutex);
  3216. ret = -EBUSY;
  3217. goto pre_mutex_err;
  3218. }
  3219. /* Look up object handles */
  3220. flips = 0;
  3221. for (i = 0; i < args->buffer_count; i++) {
  3222. object_list[i] = drm_gem_object_lookup(dev, file_priv,
  3223. exec_list[i].handle);
  3224. if (object_list[i] == NULL) {
  3225. DRM_ERROR("Invalid object handle %d at index %d\n",
  3226. exec_list[i].handle, i);
  3227. /* prevent error path from reading uninitialized data */
  3228. args->buffer_count = i + 1;
  3229. ret = -EBADF;
  3230. goto err;
  3231. }
  3232. obj_priv = object_list[i]->driver_private;
  3233. if (obj_priv->in_execbuffer) {
  3234. DRM_ERROR("Object %p appears more than once in object list\n",
  3235. object_list[i]);
  3236. /* prevent error path from reading uninitialized data */
  3237. args->buffer_count = i + 1;
  3238. ret = -EBADF;
  3239. goto err;
  3240. }
  3241. obj_priv->in_execbuffer = true;
  3242. flips += atomic_read(&obj_priv->pending_flip);
  3243. }
  3244. if (flips > 0) {
  3245. ret = i915_gem_wait_for_pending_flip(dev, object_list,
  3246. args->buffer_count);
  3247. if (ret)
  3248. goto err;
  3249. }
  3250. /* Pin and relocate */
  3251. for (pin_tries = 0; ; pin_tries++) {
  3252. ret = 0;
  3253. reloc_index = 0;
  3254. for (i = 0; i < args->buffer_count; i++) {
  3255. object_list[i]->pending_read_domains = 0;
  3256. object_list[i]->pending_write_domain = 0;
  3257. ret = i915_gem_object_pin_and_relocate(object_list[i],
  3258. file_priv,
  3259. &exec_list[i],
  3260. &relocs[reloc_index]);
  3261. if (ret)
  3262. break;
  3263. pinned = i + 1;
  3264. reloc_index += exec_list[i].relocation_count;
  3265. }
  3266. /* success */
  3267. if (ret == 0)
  3268. break;
  3269. /* error other than GTT full, or we've already tried again */
  3270. if (ret != -ENOSPC || pin_tries >= 1) {
  3271. if (ret != -ERESTARTSYS) {
  3272. unsigned long long total_size = 0;
  3273. for (i = 0; i < args->buffer_count; i++)
  3274. total_size += object_list[i]->size;
  3275. DRM_ERROR("Failed to pin buffer %d of %d, total %llu bytes: %d\n",
  3276. pinned+1, args->buffer_count,
  3277. total_size, ret);
  3278. DRM_ERROR("%d objects [%d pinned], "
  3279. "%d object bytes [%d pinned], "
  3280. "%d/%d gtt bytes\n",
  3281. atomic_read(&dev->object_count),
  3282. atomic_read(&dev->pin_count),
  3283. atomic_read(&dev->object_memory),
  3284. atomic_read(&dev->pin_memory),
  3285. atomic_read(&dev->gtt_memory),
  3286. dev->gtt_total);
  3287. }
  3288. goto err;
  3289. }
  3290. /* unpin all of our buffers */
  3291. for (i = 0; i < pinned; i++)
  3292. i915_gem_object_unpin(object_list[i]);
  3293. pinned = 0;
  3294. /* evict everyone we can from the aperture */
  3295. ret = i915_gem_evict_everything(dev);
  3296. if (ret && ret != -ENOSPC)
  3297. goto err;
  3298. }
  3299. /* Set the pending read domains for the batch buffer to COMMAND */
  3300. batch_obj = object_list[args->buffer_count-1];
  3301. if (batch_obj->pending_write_domain) {
  3302. DRM_ERROR("Attempting to use self-modifying batch buffer\n");
  3303. ret = -EINVAL;
  3304. goto err;
  3305. }
  3306. batch_obj->pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
  3307. /* Sanity check the batch buffer, prior to moving objects */
  3308. exec_offset = exec_list[args->buffer_count - 1].offset;
  3309. ret = i915_gem_check_execbuffer (args, exec_offset);
  3310. if (ret != 0) {
  3311. DRM_ERROR("execbuf with invalid offset/length\n");
  3312. goto err;
  3313. }
  3314. i915_verify_inactive(dev, __FILE__, __LINE__);
  3315. /* Zero the global flush/invalidate flags. These
  3316. * will be modified as new domains are computed
  3317. * for each object
  3318. */
  3319. dev->invalidate_domains = 0;
  3320. dev->flush_domains = 0;
  3321. for (i = 0; i < args->buffer_count; i++) {
  3322. struct drm_gem_object *obj = object_list[i];
  3323. /* Compute new gpu domains and update invalidate/flush */
  3324. i915_gem_object_set_to_gpu_domain(obj);
  3325. }
  3326. i915_verify_inactive(dev, __FILE__, __LINE__);
  3327. if (dev->invalidate_domains | dev->flush_domains) {
  3328. #if WATCH_EXEC
  3329. DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n",
  3330. __func__,
  3331. dev->invalidate_domains,
  3332. dev->flush_domains);
  3333. #endif
  3334. i915_gem_flush(dev,
  3335. dev->invalidate_domains,
  3336. dev->flush_domains);
  3337. if (dev->flush_domains & I915_GEM_GPU_DOMAINS)
  3338. (void)i915_add_request(dev, file_priv,
  3339. dev->flush_domains);
  3340. }
  3341. for (i = 0; i < args->buffer_count; i++) {
  3342. struct drm_gem_object *obj = object_list[i];
  3343. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  3344. uint32_t old_write_domain = obj->write_domain;
  3345. obj->write_domain = obj->pending_write_domain;
  3346. if (obj->write_domain)
  3347. list_move_tail(&obj_priv->gpu_write_list,
  3348. &dev_priv->mm.gpu_write_list);
  3349. else
  3350. list_del_init(&obj_priv->gpu_write_list);
  3351. trace_i915_gem_object_change_domain(obj,
  3352. obj->read_domains,
  3353. old_write_domain);
  3354. }
  3355. i915_verify_inactive(dev, __FILE__, __LINE__);
  3356. #if WATCH_COHERENCY
  3357. for (i = 0; i < args->buffer_count; i++) {
  3358. i915_gem_object_check_coherency(object_list[i],
  3359. exec_list[i].handle);
  3360. }
  3361. #endif
  3362. #if WATCH_EXEC
  3363. i915_gem_dump_object(batch_obj,
  3364. args->batch_len,
  3365. __func__,
  3366. ~0);
  3367. #endif
  3368. /* Exec the batchbuffer */
  3369. ret = i915_dispatch_gem_execbuffer(dev, args, cliprects, exec_offset);
  3370. if (ret) {
  3371. DRM_ERROR("dispatch failed %d\n", ret);
  3372. goto err;
  3373. }
  3374. /*
  3375. * Ensure that the commands in the batch buffer are
  3376. * finished before the interrupt fires
  3377. */
  3378. flush_domains = i915_retire_commands(dev);
  3379. i915_verify_inactive(dev, __FILE__, __LINE__);
  3380. /*
  3381. * Get a seqno representing the execution of the current buffer,
  3382. * which we can wait on. We would like to mitigate these interrupts,
  3383. * likely by only creating seqnos occasionally (so that we have
  3384. * *some* interrupts representing completion of buffers that we can
  3385. * wait on when trying to clear up gtt space).
  3386. */
  3387. seqno = i915_add_request(dev, file_priv, flush_domains);
  3388. BUG_ON(seqno == 0);
  3389. for (i = 0; i < args->buffer_count; i++) {
  3390. struct drm_gem_object *obj = object_list[i];
  3391. i915_gem_object_move_to_active(obj, seqno);
  3392. #if WATCH_LRU
  3393. DRM_INFO("%s: move to exec list %p\n", __func__, obj);
  3394. #endif
  3395. }
  3396. #if WATCH_LRU
  3397. i915_dump_lru(dev, __func__);
  3398. #endif
  3399. i915_verify_inactive(dev, __FILE__, __LINE__);
  3400. err:
  3401. for (i = 0; i < pinned; i++)
  3402. i915_gem_object_unpin(object_list[i]);
  3403. for (i = 0; i < args->buffer_count; i++) {
  3404. if (object_list[i]) {
  3405. obj_priv = object_list[i]->driver_private;
  3406. obj_priv->in_execbuffer = false;
  3407. }
  3408. drm_gem_object_unreference(object_list[i]);
  3409. }
  3410. mutex_unlock(&dev->struct_mutex);
  3411. pre_mutex_err:
  3412. /* Copy the updated relocations out regardless of current error
  3413. * state. Failure to update the relocs would mean that the next
  3414. * time userland calls execbuf, it would do so with presumed offset
  3415. * state that didn't match the actual object state.
  3416. */
  3417. ret2 = i915_gem_put_relocs_to_user(exec_list, args->buffer_count,
  3418. relocs);
  3419. if (ret2 != 0) {
  3420. DRM_ERROR("Failed to copy relocations back out: %d\n", ret2);
  3421. if (ret == 0)
  3422. ret = ret2;
  3423. }
  3424. drm_free_large(object_list);
  3425. kfree(cliprects);
  3426. return ret;
  3427. }
  3428. /*
  3429. * Legacy execbuffer just creates an exec2 list from the original exec object
  3430. * list array and passes it to the real function.
  3431. */
  3432. int
  3433. i915_gem_execbuffer(struct drm_device *dev, void *data,
  3434. struct drm_file *file_priv)
  3435. {
  3436. struct drm_i915_gem_execbuffer *args = data;
  3437. struct drm_i915_gem_execbuffer2 exec2;
  3438. struct drm_i915_gem_exec_object *exec_list = NULL;
  3439. struct drm_i915_gem_exec_object2 *exec2_list = NULL;
  3440. int ret, i;
  3441. #if WATCH_EXEC
  3442. DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
  3443. (int) args->buffers_ptr, args->buffer_count, args->batch_len);
  3444. #endif
  3445. if (args->buffer_count < 1) {
  3446. DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
  3447. return -EINVAL;
  3448. }
  3449. /* Copy in the exec list from userland */
  3450. exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
  3451. exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
  3452. if (exec_list == NULL || exec2_list == NULL) {
  3453. DRM_ERROR("Failed to allocate exec list for %d buffers\n",
  3454. args->buffer_count);
  3455. drm_free_large(exec_list);
  3456. drm_free_large(exec2_list);
  3457. return -ENOMEM;
  3458. }
  3459. ret = copy_from_user(exec_list,
  3460. (struct drm_i915_relocation_entry __user *)
  3461. (uintptr_t) args->buffers_ptr,
  3462. sizeof(*exec_list) * args->buffer_count);
  3463. if (ret != 0) {
  3464. DRM_ERROR("copy %d exec entries failed %d\n",
  3465. args->buffer_count, ret);
  3466. drm_free_large(exec_list);
  3467. drm_free_large(exec2_list);
  3468. return -EFAULT;
  3469. }
  3470. for (i = 0; i < args->buffer_count; i++) {
  3471. exec2_list[i].handle = exec_list[i].handle;
  3472. exec2_list[i].relocation_count = exec_list[i].relocation_count;
  3473. exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
  3474. exec2_list[i].alignment = exec_list[i].alignment;
  3475. exec2_list[i].offset = exec_list[i].offset;
  3476. if (!IS_I965G(dev))
  3477. exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
  3478. else
  3479. exec2_list[i].flags = 0;
  3480. }
  3481. exec2.buffers_ptr = args->buffers_ptr;
  3482. exec2.buffer_count = args->buffer_count;
  3483. exec2.batch_start_offset = args->batch_start_offset;
  3484. exec2.batch_len = args->batch_len;
  3485. exec2.DR1 = args->DR1;
  3486. exec2.DR4 = args->DR4;
  3487. exec2.num_cliprects = args->num_cliprects;
  3488. exec2.cliprects_ptr = args->cliprects_ptr;
  3489. exec2.flags = 0;
  3490. ret = i915_gem_do_execbuffer(dev, data, file_priv, &exec2, exec2_list);
  3491. if (!ret) {
  3492. /* Copy the new buffer offsets back to the user's exec list. */
  3493. for (i = 0; i < args->buffer_count; i++)
  3494. exec_list[i].offset = exec2_list[i].offset;
  3495. /* ... and back out to userspace */
  3496. ret = copy_to_user((struct drm_i915_relocation_entry __user *)
  3497. (uintptr_t) args->buffers_ptr,
  3498. exec_list,
  3499. sizeof(*exec_list) * args->buffer_count);
  3500. if (ret) {
  3501. ret = -EFAULT;
  3502. DRM_ERROR("failed to copy %d exec entries "
  3503. "back to user (%d)\n",
  3504. args->buffer_count, ret);
  3505. }
  3506. }
  3507. drm_free_large(exec_list);
  3508. drm_free_large(exec2_list);
  3509. return ret;
  3510. }
  3511. int
  3512. i915_gem_execbuffer2(struct drm_device *dev, void *data,
  3513. struct drm_file *file_priv)
  3514. {
  3515. struct drm_i915_gem_execbuffer2 *args = data;
  3516. struct drm_i915_gem_exec_object2 *exec2_list = NULL;
  3517. int ret;
  3518. #if WATCH_EXEC
  3519. DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
  3520. (int) args->buffers_ptr, args->buffer_count, args->batch_len);
  3521. #endif
  3522. if (args->buffer_count < 1) {
  3523. DRM_ERROR("execbuf2 with %d buffers\n", args->buffer_count);
  3524. return -EINVAL;
  3525. }
  3526. exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
  3527. if (exec2_list == NULL) {
  3528. DRM_ERROR("Failed to allocate exec list for %d buffers\n",
  3529. args->buffer_count);
  3530. return -ENOMEM;
  3531. }
  3532. ret = copy_from_user(exec2_list,
  3533. (struct drm_i915_relocation_entry __user *)
  3534. (uintptr_t) args->buffers_ptr,
  3535. sizeof(*exec2_list) * args->buffer_count);
  3536. if (ret != 0) {
  3537. DRM_ERROR("copy %d exec entries failed %d\n",
  3538. args->buffer_count, ret);
  3539. drm_free_large(exec2_list);
  3540. return -EFAULT;
  3541. }
  3542. ret = i915_gem_do_execbuffer(dev, data, file_priv, args, exec2_list);
  3543. if (!ret) {
  3544. /* Copy the new buffer offsets back to the user's exec list. */
  3545. ret = copy_to_user((struct drm_i915_relocation_entry __user *)
  3546. (uintptr_t) args->buffers_ptr,
  3547. exec2_list,
  3548. sizeof(*exec2_list) * args->buffer_count);
  3549. if (ret) {
  3550. ret = -EFAULT;
  3551. DRM_ERROR("failed to copy %d exec entries "
  3552. "back to user (%d)\n",
  3553. args->buffer_count, ret);
  3554. }
  3555. }
  3556. drm_free_large(exec2_list);
  3557. return ret;
  3558. }
  3559. int
  3560. i915_gem_object_pin(struct drm_gem_object *obj, uint32_t alignment)
  3561. {
  3562. struct drm_device *dev = obj->dev;
  3563. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  3564. int ret;
  3565. i915_verify_inactive(dev, __FILE__, __LINE__);
  3566. if (obj_priv->gtt_space == NULL) {
  3567. ret = i915_gem_object_bind_to_gtt(obj, alignment);
  3568. if (ret)
  3569. return ret;
  3570. }
  3571. obj_priv->pin_count++;
  3572. /* If the object is not active and not pending a flush,
  3573. * remove it from the inactive list
  3574. */
  3575. if (obj_priv->pin_count == 1) {
  3576. atomic_inc(&dev->pin_count);
  3577. atomic_add(obj->size, &dev->pin_memory);
  3578. if (!obj_priv->active &&
  3579. (obj->write_domain & I915_GEM_GPU_DOMAINS) == 0 &&
  3580. !list_empty(&obj_priv->list))
  3581. list_del_init(&obj_priv->list);
  3582. }
  3583. i915_verify_inactive(dev, __FILE__, __LINE__);
  3584. return 0;
  3585. }
  3586. void
  3587. i915_gem_object_unpin(struct drm_gem_object *obj)
  3588. {
  3589. struct drm_device *dev = obj->dev;
  3590. drm_i915_private_t *dev_priv = dev->dev_private;
  3591. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  3592. i915_verify_inactive(dev, __FILE__, __LINE__);
  3593. obj_priv->pin_count--;
  3594. BUG_ON(obj_priv->pin_count < 0);
  3595. BUG_ON(obj_priv->gtt_space == NULL);
  3596. /* If the object is no longer pinned, and is
  3597. * neither active nor being flushed, then stick it on
  3598. * the inactive list
  3599. */
  3600. if (obj_priv->pin_count == 0) {
  3601. if (!obj_priv->active &&
  3602. (obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
  3603. list_move_tail(&obj_priv->list,
  3604. &dev_priv->mm.inactive_list);
  3605. atomic_dec(&dev->pin_count);
  3606. atomic_sub(obj->size, &dev->pin_memory);
  3607. }
  3608. i915_verify_inactive(dev, __FILE__, __LINE__);
  3609. }
  3610. int
  3611. i915_gem_pin_ioctl(struct drm_device *dev, void *data,
  3612. struct drm_file *file_priv)
  3613. {
  3614. struct drm_i915_gem_pin *args = data;
  3615. struct drm_gem_object *obj;
  3616. struct drm_i915_gem_object *obj_priv;
  3617. int ret;
  3618. mutex_lock(&dev->struct_mutex);
  3619. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  3620. if (obj == NULL) {
  3621. DRM_ERROR("Bad handle in i915_gem_pin_ioctl(): %d\n",
  3622. args->handle);
  3623. mutex_unlock(&dev->struct_mutex);
  3624. return -EBADF;
  3625. }
  3626. obj_priv = obj->driver_private;
  3627. if (obj_priv->madv != I915_MADV_WILLNEED) {
  3628. DRM_ERROR("Attempting to pin a purgeable buffer\n");
  3629. drm_gem_object_unreference(obj);
  3630. mutex_unlock(&dev->struct_mutex);
  3631. return -EINVAL;
  3632. }
  3633. if (obj_priv->pin_filp != NULL && obj_priv->pin_filp != file_priv) {
  3634. DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
  3635. args->handle);
  3636. drm_gem_object_unreference(obj);
  3637. mutex_unlock(&dev->struct_mutex);
  3638. return -EINVAL;
  3639. }
  3640. obj_priv->user_pin_count++;
  3641. obj_priv->pin_filp = file_priv;
  3642. if (obj_priv->user_pin_count == 1) {
  3643. ret = i915_gem_object_pin(obj, args->alignment);
  3644. if (ret != 0) {
  3645. drm_gem_object_unreference(obj);
  3646. mutex_unlock(&dev->struct_mutex);
  3647. return ret;
  3648. }
  3649. }
  3650. /* XXX - flush the CPU caches for pinned objects
  3651. * as the X server doesn't manage domains yet
  3652. */
  3653. i915_gem_object_flush_cpu_write_domain(obj);
  3654. args->offset = obj_priv->gtt_offset;
  3655. drm_gem_object_unreference(obj);
  3656. mutex_unlock(&dev->struct_mutex);
  3657. return 0;
  3658. }
  3659. int
  3660. i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
  3661. struct drm_file *file_priv)
  3662. {
  3663. struct drm_i915_gem_pin *args = data;
  3664. struct drm_gem_object *obj;
  3665. struct drm_i915_gem_object *obj_priv;
  3666. mutex_lock(&dev->struct_mutex);
  3667. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  3668. if (obj == NULL) {
  3669. DRM_ERROR("Bad handle in i915_gem_unpin_ioctl(): %d\n",
  3670. args->handle);
  3671. mutex_unlock(&dev->struct_mutex);
  3672. return -EBADF;
  3673. }
  3674. obj_priv = obj->driver_private;
  3675. if (obj_priv->pin_filp != file_priv) {
  3676. DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
  3677. args->handle);
  3678. drm_gem_object_unreference(obj);
  3679. mutex_unlock(&dev->struct_mutex);
  3680. return -EINVAL;
  3681. }
  3682. obj_priv->user_pin_count--;
  3683. if (obj_priv->user_pin_count == 0) {
  3684. obj_priv->pin_filp = NULL;
  3685. i915_gem_object_unpin(obj);
  3686. }
  3687. drm_gem_object_unreference(obj);
  3688. mutex_unlock(&dev->struct_mutex);
  3689. return 0;
  3690. }
  3691. int
  3692. i915_gem_busy_ioctl(struct drm_device *dev, void *data,
  3693. struct drm_file *file_priv)
  3694. {
  3695. struct drm_i915_gem_busy *args = data;
  3696. struct drm_gem_object *obj;
  3697. struct drm_i915_gem_object *obj_priv;
  3698. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  3699. if (obj == NULL) {
  3700. DRM_ERROR("Bad handle in i915_gem_busy_ioctl(): %d\n",
  3701. args->handle);
  3702. return -EBADF;
  3703. }
  3704. mutex_lock(&dev->struct_mutex);
  3705. /* Update the active list for the hardware's current position.
  3706. * Otherwise this only updates on a delayed timer or when irqs are
  3707. * actually unmasked, and our working set ends up being larger than
  3708. * required.
  3709. */
  3710. i915_gem_retire_requests(dev);
  3711. obj_priv = obj->driver_private;
  3712. /* Don't count being on the flushing list against the object being
  3713. * done. Otherwise, a buffer left on the flushing list but not getting
  3714. * flushed (because nobody's flushing that domain) won't ever return
  3715. * unbusy and get reused by libdrm's bo cache. The other expected
  3716. * consumer of this interface, OpenGL's occlusion queries, also specs
  3717. * that the objects get unbusy "eventually" without any interference.
  3718. */
  3719. args->busy = obj_priv->active && obj_priv->last_rendering_seqno != 0;
  3720. drm_gem_object_unreference(obj);
  3721. mutex_unlock(&dev->struct_mutex);
  3722. return 0;
  3723. }
  3724. int
  3725. i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
  3726. struct drm_file *file_priv)
  3727. {
  3728. return i915_gem_ring_throttle(dev, file_priv);
  3729. }
  3730. int
  3731. i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
  3732. struct drm_file *file_priv)
  3733. {
  3734. struct drm_i915_gem_madvise *args = data;
  3735. struct drm_gem_object *obj;
  3736. struct drm_i915_gem_object *obj_priv;
  3737. switch (args->madv) {
  3738. case I915_MADV_DONTNEED:
  3739. case I915_MADV_WILLNEED:
  3740. break;
  3741. default:
  3742. return -EINVAL;
  3743. }
  3744. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  3745. if (obj == NULL) {
  3746. DRM_ERROR("Bad handle in i915_gem_madvise_ioctl(): %d\n",
  3747. args->handle);
  3748. return -EBADF;
  3749. }
  3750. mutex_lock(&dev->struct_mutex);
  3751. obj_priv = obj->driver_private;
  3752. if (obj_priv->pin_count) {
  3753. drm_gem_object_unreference(obj);
  3754. mutex_unlock(&dev->struct_mutex);
  3755. DRM_ERROR("Attempted i915_gem_madvise_ioctl() on a pinned object\n");
  3756. return -EINVAL;
  3757. }
  3758. if (obj_priv->madv != __I915_MADV_PURGED)
  3759. obj_priv->madv = args->madv;
  3760. /* if the object is no longer bound, discard its backing storage */
  3761. if (i915_gem_object_is_purgeable(obj_priv) &&
  3762. obj_priv->gtt_space == NULL)
  3763. i915_gem_object_truncate(obj);
  3764. args->retained = obj_priv->madv != __I915_MADV_PURGED;
  3765. drm_gem_object_unreference(obj);
  3766. mutex_unlock(&dev->struct_mutex);
  3767. return 0;
  3768. }
  3769. int i915_gem_init_object(struct drm_gem_object *obj)
  3770. {
  3771. struct drm_i915_gem_object *obj_priv;
  3772. obj_priv = kzalloc(sizeof(*obj_priv), GFP_KERNEL);
  3773. if (obj_priv == NULL)
  3774. return -ENOMEM;
  3775. /*
  3776. * We've just allocated pages from the kernel,
  3777. * so they've just been written by the CPU with
  3778. * zeros. They'll need to be clflushed before we
  3779. * use them with the GPU.
  3780. */
  3781. obj->write_domain = I915_GEM_DOMAIN_CPU;
  3782. obj->read_domains = I915_GEM_DOMAIN_CPU;
  3783. obj_priv->agp_type = AGP_USER_MEMORY;
  3784. obj->driver_private = obj_priv;
  3785. obj_priv->obj = obj;
  3786. obj_priv->fence_reg = I915_FENCE_REG_NONE;
  3787. INIT_LIST_HEAD(&obj_priv->list);
  3788. INIT_LIST_HEAD(&obj_priv->gpu_write_list);
  3789. INIT_LIST_HEAD(&obj_priv->fence_list);
  3790. obj_priv->madv = I915_MADV_WILLNEED;
  3791. trace_i915_gem_object_create(obj);
  3792. return 0;
  3793. }
  3794. void i915_gem_free_object(struct drm_gem_object *obj)
  3795. {
  3796. struct drm_device *dev = obj->dev;
  3797. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  3798. trace_i915_gem_object_destroy(obj);
  3799. while (obj_priv->pin_count > 0)
  3800. i915_gem_object_unpin(obj);
  3801. if (obj_priv->phys_obj)
  3802. i915_gem_detach_phys_object(dev, obj);
  3803. i915_gem_object_unbind(obj);
  3804. if (obj_priv->mmap_offset)
  3805. i915_gem_free_mmap_offset(obj);
  3806. kfree(obj_priv->page_cpu_valid);
  3807. kfree(obj_priv->bit_17);
  3808. kfree(obj->driver_private);
  3809. }
  3810. /** Unbinds all inactive objects. */
  3811. static int
  3812. i915_gem_evict_from_inactive_list(struct drm_device *dev)
  3813. {
  3814. drm_i915_private_t *dev_priv = dev->dev_private;
  3815. while (!list_empty(&dev_priv->mm.inactive_list)) {
  3816. struct drm_gem_object *obj;
  3817. int ret;
  3818. obj = list_first_entry(&dev_priv->mm.inactive_list,
  3819. struct drm_i915_gem_object,
  3820. list)->obj;
  3821. ret = i915_gem_object_unbind(obj);
  3822. if (ret != 0) {
  3823. DRM_ERROR("Error unbinding object: %d\n", ret);
  3824. return ret;
  3825. }
  3826. }
  3827. return 0;
  3828. }
  3829. int
  3830. i915_gem_idle(struct drm_device *dev)
  3831. {
  3832. drm_i915_private_t *dev_priv = dev->dev_private;
  3833. int ret;
  3834. mutex_lock(&dev->struct_mutex);
  3835. if (dev_priv->mm.suspended || dev_priv->ring.ring_obj == NULL) {
  3836. mutex_unlock(&dev->struct_mutex);
  3837. return 0;
  3838. }
  3839. ret = i915_gpu_idle(dev);
  3840. if (ret) {
  3841. mutex_unlock(&dev->struct_mutex);
  3842. return ret;
  3843. }
  3844. /* Under UMS, be paranoid and evict. */
  3845. if (!drm_core_check_feature(dev, DRIVER_MODESET)) {
  3846. ret = i915_gem_evict_from_inactive_list(dev);
  3847. if (ret) {
  3848. mutex_unlock(&dev->struct_mutex);
  3849. return ret;
  3850. }
  3851. }
  3852. /* Hack! Don't let anybody do execbuf while we don't control the chip.
  3853. * We need to replace this with a semaphore, or something.
  3854. * And not confound mm.suspended!
  3855. */
  3856. dev_priv->mm.suspended = 1;
  3857. del_timer(&dev_priv->hangcheck_timer);
  3858. i915_kernel_lost_context(dev);
  3859. i915_gem_cleanup_ringbuffer(dev);
  3860. mutex_unlock(&dev->struct_mutex);
  3861. /* Cancel the retire work handler, which should be idle now. */
  3862. cancel_delayed_work_sync(&dev_priv->mm.retire_work);
  3863. return 0;
  3864. }
  3865. static int
  3866. i915_gem_init_hws(struct drm_device *dev)
  3867. {
  3868. drm_i915_private_t *dev_priv = dev->dev_private;
  3869. struct drm_gem_object *obj;
  3870. struct drm_i915_gem_object *obj_priv;
  3871. int ret;
  3872. /* If we need a physical address for the status page, it's already
  3873. * initialized at driver load time.
  3874. */
  3875. if (!I915_NEED_GFX_HWS(dev))
  3876. return 0;
  3877. obj = drm_gem_object_alloc(dev, 4096);
  3878. if (obj == NULL) {
  3879. DRM_ERROR("Failed to allocate status page\n");
  3880. return -ENOMEM;
  3881. }
  3882. obj_priv = obj->driver_private;
  3883. obj_priv->agp_type = AGP_USER_CACHED_MEMORY;
  3884. ret = i915_gem_object_pin(obj, 4096);
  3885. if (ret != 0) {
  3886. drm_gem_object_unreference(obj);
  3887. return ret;
  3888. }
  3889. dev_priv->status_gfx_addr = obj_priv->gtt_offset;
  3890. dev_priv->hw_status_page = kmap(obj_priv->pages[0]);
  3891. if (dev_priv->hw_status_page == NULL) {
  3892. DRM_ERROR("Failed to map status page.\n");
  3893. memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
  3894. i915_gem_object_unpin(obj);
  3895. drm_gem_object_unreference(obj);
  3896. return -EINVAL;
  3897. }
  3898. dev_priv->hws_obj = obj;
  3899. memset(dev_priv->hw_status_page, 0, PAGE_SIZE);
  3900. if (IS_GEN6(dev)) {
  3901. I915_WRITE(HWS_PGA_GEN6, dev_priv->status_gfx_addr);
  3902. I915_READ(HWS_PGA_GEN6); /* posting read */
  3903. } else {
  3904. I915_WRITE(HWS_PGA, dev_priv->status_gfx_addr);
  3905. I915_READ(HWS_PGA); /* posting read */
  3906. }
  3907. DRM_DEBUG_DRIVER("hws offset: 0x%08x\n", dev_priv->status_gfx_addr);
  3908. return 0;
  3909. }
  3910. static void
  3911. i915_gem_cleanup_hws(struct drm_device *dev)
  3912. {
  3913. drm_i915_private_t *dev_priv = dev->dev_private;
  3914. struct drm_gem_object *obj;
  3915. struct drm_i915_gem_object *obj_priv;
  3916. if (dev_priv->hws_obj == NULL)
  3917. return;
  3918. obj = dev_priv->hws_obj;
  3919. obj_priv = obj->driver_private;
  3920. kunmap(obj_priv->pages[0]);
  3921. i915_gem_object_unpin(obj);
  3922. drm_gem_object_unreference(obj);
  3923. dev_priv->hws_obj = NULL;
  3924. memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map));
  3925. dev_priv->hw_status_page = NULL;
  3926. /* Write high address into HWS_PGA when disabling. */
  3927. I915_WRITE(HWS_PGA, 0x1ffff000);
  3928. }
  3929. int
  3930. i915_gem_init_ringbuffer(struct drm_device *dev)
  3931. {
  3932. drm_i915_private_t *dev_priv = dev->dev_private;
  3933. struct drm_gem_object *obj;
  3934. struct drm_i915_gem_object *obj_priv;
  3935. drm_i915_ring_buffer_t *ring = &dev_priv->ring;
  3936. int ret;
  3937. u32 head;
  3938. ret = i915_gem_init_hws(dev);
  3939. if (ret != 0)
  3940. return ret;
  3941. obj = drm_gem_object_alloc(dev, 128 * 1024);
  3942. if (obj == NULL) {
  3943. DRM_ERROR("Failed to allocate ringbuffer\n");
  3944. i915_gem_cleanup_hws(dev);
  3945. return -ENOMEM;
  3946. }
  3947. obj_priv = obj->driver_private;
  3948. ret = i915_gem_object_pin(obj, 4096);
  3949. if (ret != 0) {
  3950. drm_gem_object_unreference(obj);
  3951. i915_gem_cleanup_hws(dev);
  3952. return ret;
  3953. }
  3954. /* Set up the kernel mapping for the ring. */
  3955. ring->Size = obj->size;
  3956. ring->map.offset = dev->agp->base + obj_priv->gtt_offset;
  3957. ring->map.size = obj->size;
  3958. ring->map.type = 0;
  3959. ring->map.flags = 0;
  3960. ring->map.mtrr = 0;
  3961. drm_core_ioremap_wc(&ring->map, dev);
  3962. if (ring->map.handle == NULL) {
  3963. DRM_ERROR("Failed to map ringbuffer.\n");
  3964. memset(&dev_priv->ring, 0, sizeof(dev_priv->ring));
  3965. i915_gem_object_unpin(obj);
  3966. drm_gem_object_unreference(obj);
  3967. i915_gem_cleanup_hws(dev);
  3968. return -EINVAL;
  3969. }
  3970. ring->ring_obj = obj;
  3971. ring->virtual_start = ring->map.handle;
  3972. /* Stop the ring if it's running. */
  3973. I915_WRITE(PRB0_CTL, 0);
  3974. I915_WRITE(PRB0_TAIL, 0);
  3975. I915_WRITE(PRB0_HEAD, 0);
  3976. /* Initialize the ring. */
  3977. I915_WRITE(PRB0_START, obj_priv->gtt_offset);
  3978. head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
  3979. /* G45 ring initialization fails to reset head to zero */
  3980. if (head != 0) {
  3981. DRM_ERROR("Ring head not reset to zero "
  3982. "ctl %08x head %08x tail %08x start %08x\n",
  3983. I915_READ(PRB0_CTL),
  3984. I915_READ(PRB0_HEAD),
  3985. I915_READ(PRB0_TAIL),
  3986. I915_READ(PRB0_START));
  3987. I915_WRITE(PRB0_HEAD, 0);
  3988. DRM_ERROR("Ring head forced to zero "
  3989. "ctl %08x head %08x tail %08x start %08x\n",
  3990. I915_READ(PRB0_CTL),
  3991. I915_READ(PRB0_HEAD),
  3992. I915_READ(PRB0_TAIL),
  3993. I915_READ(PRB0_START));
  3994. }
  3995. I915_WRITE(PRB0_CTL,
  3996. ((obj->size - 4096) & RING_NR_PAGES) |
  3997. RING_NO_REPORT |
  3998. RING_VALID);
  3999. head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
  4000. /* If the head is still not zero, the ring is dead */
  4001. if (head != 0) {
  4002. DRM_ERROR("Ring initialization failed "
  4003. "ctl %08x head %08x tail %08x start %08x\n",
  4004. I915_READ(PRB0_CTL),
  4005. I915_READ(PRB0_HEAD),
  4006. I915_READ(PRB0_TAIL),
  4007. I915_READ(PRB0_START));
  4008. return -EIO;
  4009. }
  4010. /* Update our cache of the ring state */
  4011. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  4012. i915_kernel_lost_context(dev);
  4013. else {
  4014. ring->head = I915_READ(PRB0_HEAD) & HEAD_ADDR;
  4015. ring->tail = I915_READ(PRB0_TAIL) & TAIL_ADDR;
  4016. ring->space = ring->head - (ring->tail + 8);
  4017. if (ring->space < 0)
  4018. ring->space += ring->Size;
  4019. }
  4020. return 0;
  4021. }
  4022. void
  4023. i915_gem_cleanup_ringbuffer(struct drm_device *dev)
  4024. {
  4025. drm_i915_private_t *dev_priv = dev->dev_private;
  4026. if (dev_priv->ring.ring_obj == NULL)
  4027. return;
  4028. drm_core_ioremapfree(&dev_priv->ring.map, dev);
  4029. i915_gem_object_unpin(dev_priv->ring.ring_obj);
  4030. drm_gem_object_unreference(dev_priv->ring.ring_obj);
  4031. dev_priv->ring.ring_obj = NULL;
  4032. memset(&dev_priv->ring, 0, sizeof(dev_priv->ring));
  4033. i915_gem_cleanup_hws(dev);
  4034. }
  4035. int
  4036. i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
  4037. struct drm_file *file_priv)
  4038. {
  4039. drm_i915_private_t *dev_priv = dev->dev_private;
  4040. int ret;
  4041. if (drm_core_check_feature(dev, DRIVER_MODESET))
  4042. return 0;
  4043. if (atomic_read(&dev_priv->mm.wedged)) {
  4044. DRM_ERROR("Reenabling wedged hardware, good luck\n");
  4045. atomic_set(&dev_priv->mm.wedged, 0);
  4046. }
  4047. mutex_lock(&dev->struct_mutex);
  4048. dev_priv->mm.suspended = 0;
  4049. ret = i915_gem_init_ringbuffer(dev);
  4050. if (ret != 0) {
  4051. mutex_unlock(&dev->struct_mutex);
  4052. return ret;
  4053. }
  4054. spin_lock(&dev_priv->mm.active_list_lock);
  4055. BUG_ON(!list_empty(&dev_priv->mm.active_list));
  4056. spin_unlock(&dev_priv->mm.active_list_lock);
  4057. BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
  4058. BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
  4059. BUG_ON(!list_empty(&dev_priv->mm.request_list));
  4060. mutex_unlock(&dev->struct_mutex);
  4061. drm_irq_install(dev);
  4062. return 0;
  4063. }
  4064. int
  4065. i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
  4066. struct drm_file *file_priv)
  4067. {
  4068. if (drm_core_check_feature(dev, DRIVER_MODESET))
  4069. return 0;
  4070. drm_irq_uninstall(dev);
  4071. return i915_gem_idle(dev);
  4072. }
  4073. void
  4074. i915_gem_lastclose(struct drm_device *dev)
  4075. {
  4076. int ret;
  4077. if (drm_core_check_feature(dev, DRIVER_MODESET))
  4078. return;
  4079. ret = i915_gem_idle(dev);
  4080. if (ret)
  4081. DRM_ERROR("failed to idle hardware: %d\n", ret);
  4082. }
  4083. void
  4084. i915_gem_load(struct drm_device *dev)
  4085. {
  4086. int i;
  4087. drm_i915_private_t *dev_priv = dev->dev_private;
  4088. spin_lock_init(&dev_priv->mm.active_list_lock);
  4089. INIT_LIST_HEAD(&dev_priv->mm.active_list);
  4090. INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
  4091. INIT_LIST_HEAD(&dev_priv->mm.gpu_write_list);
  4092. INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
  4093. INIT_LIST_HEAD(&dev_priv->mm.request_list);
  4094. INIT_LIST_HEAD(&dev_priv->mm.fence_list);
  4095. INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
  4096. i915_gem_retire_work_handler);
  4097. dev_priv->mm.next_gem_seqno = 1;
  4098. spin_lock(&shrink_list_lock);
  4099. list_add(&dev_priv->mm.shrink_list, &shrink_list);
  4100. spin_unlock(&shrink_list_lock);
  4101. /* Old X drivers will take 0-2 for front, back, depth buffers */
  4102. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  4103. dev_priv->fence_reg_start = 3;
  4104. if (IS_I965G(dev) || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
  4105. dev_priv->num_fence_regs = 16;
  4106. else
  4107. dev_priv->num_fence_regs = 8;
  4108. /* Initialize fence registers to zero */
  4109. if (IS_I965G(dev)) {
  4110. for (i = 0; i < 16; i++)
  4111. I915_WRITE64(FENCE_REG_965_0 + (i * 8), 0);
  4112. } else {
  4113. for (i = 0; i < 8; i++)
  4114. I915_WRITE(FENCE_REG_830_0 + (i * 4), 0);
  4115. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
  4116. for (i = 0; i < 8; i++)
  4117. I915_WRITE(FENCE_REG_945_8 + (i * 4), 0);
  4118. }
  4119. i915_gem_detect_bit_6_swizzle(dev);
  4120. init_waitqueue_head(&dev_priv->pending_flip_queue);
  4121. }
  4122. /*
  4123. * Create a physically contiguous memory object for this object
  4124. * e.g. for cursor + overlay regs
  4125. */
  4126. int i915_gem_init_phys_object(struct drm_device *dev,
  4127. int id, int size)
  4128. {
  4129. drm_i915_private_t *dev_priv = dev->dev_private;
  4130. struct drm_i915_gem_phys_object *phys_obj;
  4131. int ret;
  4132. if (dev_priv->mm.phys_objs[id - 1] || !size)
  4133. return 0;
  4134. phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
  4135. if (!phys_obj)
  4136. return -ENOMEM;
  4137. phys_obj->id = id;
  4138. phys_obj->handle = drm_pci_alloc(dev, size, 0);
  4139. if (!phys_obj->handle) {
  4140. ret = -ENOMEM;
  4141. goto kfree_obj;
  4142. }
  4143. #ifdef CONFIG_X86
  4144. set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
  4145. #endif
  4146. dev_priv->mm.phys_objs[id - 1] = phys_obj;
  4147. return 0;
  4148. kfree_obj:
  4149. kfree(phys_obj);
  4150. return ret;
  4151. }
  4152. void i915_gem_free_phys_object(struct drm_device *dev, int id)
  4153. {
  4154. drm_i915_private_t *dev_priv = dev->dev_private;
  4155. struct drm_i915_gem_phys_object *phys_obj;
  4156. if (!dev_priv->mm.phys_objs[id - 1])
  4157. return;
  4158. phys_obj = dev_priv->mm.phys_objs[id - 1];
  4159. if (phys_obj->cur_obj) {
  4160. i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
  4161. }
  4162. #ifdef CONFIG_X86
  4163. set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
  4164. #endif
  4165. drm_pci_free(dev, phys_obj->handle);
  4166. kfree(phys_obj);
  4167. dev_priv->mm.phys_objs[id - 1] = NULL;
  4168. }
  4169. void i915_gem_free_all_phys_object(struct drm_device *dev)
  4170. {
  4171. int i;
  4172. for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
  4173. i915_gem_free_phys_object(dev, i);
  4174. }
  4175. void i915_gem_detach_phys_object(struct drm_device *dev,
  4176. struct drm_gem_object *obj)
  4177. {
  4178. struct drm_i915_gem_object *obj_priv;
  4179. int i;
  4180. int ret;
  4181. int page_count;
  4182. obj_priv = obj->driver_private;
  4183. if (!obj_priv->phys_obj)
  4184. return;
  4185. ret = i915_gem_object_get_pages(obj, 0);
  4186. if (ret)
  4187. goto out;
  4188. page_count = obj->size / PAGE_SIZE;
  4189. for (i = 0; i < page_count; i++) {
  4190. char *dst = kmap_atomic(obj_priv->pages[i], KM_USER0);
  4191. char *src = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
  4192. memcpy(dst, src, PAGE_SIZE);
  4193. kunmap_atomic(dst, KM_USER0);
  4194. }
  4195. drm_clflush_pages(obj_priv->pages, page_count);
  4196. drm_agp_chipset_flush(dev);
  4197. i915_gem_object_put_pages(obj);
  4198. out:
  4199. obj_priv->phys_obj->cur_obj = NULL;
  4200. obj_priv->phys_obj = NULL;
  4201. }
  4202. int
  4203. i915_gem_attach_phys_object(struct drm_device *dev,
  4204. struct drm_gem_object *obj, int id)
  4205. {
  4206. drm_i915_private_t *dev_priv = dev->dev_private;
  4207. struct drm_i915_gem_object *obj_priv;
  4208. int ret = 0;
  4209. int page_count;
  4210. int i;
  4211. if (id > I915_MAX_PHYS_OBJECT)
  4212. return -EINVAL;
  4213. obj_priv = obj->driver_private;
  4214. if (obj_priv->phys_obj) {
  4215. if (obj_priv->phys_obj->id == id)
  4216. return 0;
  4217. i915_gem_detach_phys_object(dev, obj);
  4218. }
  4219. /* create a new object */
  4220. if (!dev_priv->mm.phys_objs[id - 1]) {
  4221. ret = i915_gem_init_phys_object(dev, id,
  4222. obj->size);
  4223. if (ret) {
  4224. DRM_ERROR("failed to init phys object %d size: %zu\n", id, obj->size);
  4225. goto out;
  4226. }
  4227. }
  4228. /* bind to the object */
  4229. obj_priv->phys_obj = dev_priv->mm.phys_objs[id - 1];
  4230. obj_priv->phys_obj->cur_obj = obj;
  4231. ret = i915_gem_object_get_pages(obj, 0);
  4232. if (ret) {
  4233. DRM_ERROR("failed to get page list\n");
  4234. goto out;
  4235. }
  4236. page_count = obj->size / PAGE_SIZE;
  4237. for (i = 0; i < page_count; i++) {
  4238. char *src = kmap_atomic(obj_priv->pages[i], KM_USER0);
  4239. char *dst = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
  4240. memcpy(dst, src, PAGE_SIZE);
  4241. kunmap_atomic(src, KM_USER0);
  4242. }
  4243. i915_gem_object_put_pages(obj);
  4244. return 0;
  4245. out:
  4246. return ret;
  4247. }
  4248. static int
  4249. i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
  4250. struct drm_i915_gem_pwrite *args,
  4251. struct drm_file *file_priv)
  4252. {
  4253. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  4254. void *obj_addr;
  4255. int ret;
  4256. char __user *user_data;
  4257. user_data = (char __user *) (uintptr_t) args->data_ptr;
  4258. obj_addr = obj_priv->phys_obj->handle->vaddr + args->offset;
  4259. DRM_DEBUG_DRIVER("obj_addr %p, %lld\n", obj_addr, args->size);
  4260. ret = copy_from_user(obj_addr, user_data, args->size);
  4261. if (ret)
  4262. return -EFAULT;
  4263. drm_agp_chipset_flush(dev);
  4264. return 0;
  4265. }
  4266. void i915_gem_release(struct drm_device * dev, struct drm_file *file_priv)
  4267. {
  4268. struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv;
  4269. /* Clean up our request list when the client is going away, so that
  4270. * later retire_requests won't dereference our soon-to-be-gone
  4271. * file_priv.
  4272. */
  4273. mutex_lock(&dev->struct_mutex);
  4274. while (!list_empty(&i915_file_priv->mm.request_list))
  4275. list_del_init(i915_file_priv->mm.request_list.next);
  4276. mutex_unlock(&dev->struct_mutex);
  4277. }
  4278. static int
  4279. i915_gem_shrink(int nr_to_scan, gfp_t gfp_mask)
  4280. {
  4281. drm_i915_private_t *dev_priv, *next_dev;
  4282. struct drm_i915_gem_object *obj_priv, *next_obj;
  4283. int cnt = 0;
  4284. int would_deadlock = 1;
  4285. /* "fast-path" to count number of available objects */
  4286. if (nr_to_scan == 0) {
  4287. spin_lock(&shrink_list_lock);
  4288. list_for_each_entry(dev_priv, &shrink_list, mm.shrink_list) {
  4289. struct drm_device *dev = dev_priv->dev;
  4290. if (mutex_trylock(&dev->struct_mutex)) {
  4291. list_for_each_entry(obj_priv,
  4292. &dev_priv->mm.inactive_list,
  4293. list)
  4294. cnt++;
  4295. mutex_unlock(&dev->struct_mutex);
  4296. }
  4297. }
  4298. spin_unlock(&shrink_list_lock);
  4299. return (cnt / 100) * sysctl_vfs_cache_pressure;
  4300. }
  4301. spin_lock(&shrink_list_lock);
  4302. /* first scan for clean buffers */
  4303. list_for_each_entry_safe(dev_priv, next_dev,
  4304. &shrink_list, mm.shrink_list) {
  4305. struct drm_device *dev = dev_priv->dev;
  4306. if (! mutex_trylock(&dev->struct_mutex))
  4307. continue;
  4308. spin_unlock(&shrink_list_lock);
  4309. i915_gem_retire_requests(dev);
  4310. list_for_each_entry_safe(obj_priv, next_obj,
  4311. &dev_priv->mm.inactive_list,
  4312. list) {
  4313. if (i915_gem_object_is_purgeable(obj_priv)) {
  4314. i915_gem_object_unbind(obj_priv->obj);
  4315. if (--nr_to_scan <= 0)
  4316. break;
  4317. }
  4318. }
  4319. spin_lock(&shrink_list_lock);
  4320. mutex_unlock(&dev->struct_mutex);
  4321. would_deadlock = 0;
  4322. if (nr_to_scan <= 0)
  4323. break;
  4324. }
  4325. /* second pass, evict/count anything still on the inactive list */
  4326. list_for_each_entry_safe(dev_priv, next_dev,
  4327. &shrink_list, mm.shrink_list) {
  4328. struct drm_device *dev = dev_priv->dev;
  4329. if (! mutex_trylock(&dev->struct_mutex))
  4330. continue;
  4331. spin_unlock(&shrink_list_lock);
  4332. list_for_each_entry_safe(obj_priv, next_obj,
  4333. &dev_priv->mm.inactive_list,
  4334. list) {
  4335. if (nr_to_scan > 0) {
  4336. i915_gem_object_unbind(obj_priv->obj);
  4337. nr_to_scan--;
  4338. } else
  4339. cnt++;
  4340. }
  4341. spin_lock(&shrink_list_lock);
  4342. mutex_unlock(&dev->struct_mutex);
  4343. would_deadlock = 0;
  4344. }
  4345. spin_unlock(&shrink_list_lock);
  4346. if (would_deadlock)
  4347. return -1;
  4348. else if (cnt > 0)
  4349. return (cnt / 100) * sysctl_vfs_cache_pressure;
  4350. else
  4351. return 0;
  4352. }
  4353. static struct shrinker shrinker = {
  4354. .shrink = i915_gem_shrink,
  4355. .seeks = DEFAULT_SEEKS,
  4356. };
  4357. __init void
  4358. i915_gem_shrinker_init(void)
  4359. {
  4360. register_shrinker(&shrinker);
  4361. }
  4362. __exit void
  4363. i915_gem_shrinker_exit(void)
  4364. {
  4365. unregister_shrinker(&shrinker);
  4366. }