i915_gem.c 135 KB

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