i915_gem.c 135 KB

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