i915_gem.c 135 KB

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